Bug fixes, Start supporting google awareness API

This commit is contained in:
Tommaso Berlose
2017-10-20 12:30:28 +02:00
parent 42dbe7e235
commit 1494c2bc1e
22 changed files with 200 additions and 113 deletions

View File

@ -14,6 +14,7 @@ object Constants {
val LOCATION_REQUEST_CODE = 2
val CALENDAR_APP_REQUEST_CODE = 3
val WEATHER_APP_REQUEST_CODE = 4
val EVENT_APP_REQUEST_CODE = 6
val WEATHER_PROVIDER_REQUEST_CODE = 5
@ -48,6 +49,8 @@ object Constants {
val PREF_WEATHER_APP_NAME = "PREF_WEATHER_APP_NAME"
val PREF_WEATHER_APP_PACKAGE = "PREF_WEATHER_APP_PACKAGE"
val PREF_WEATHER_PROVIDER_API_KEY = "PREF_WEATHER_PROVIDER_API_KEY"
val PREF_EVENT_APP_NAME = "PREF_EVENT_APP_NAME"
val PREF_EVENT_APP_PACKAGE = "PREF_EVENT_APP_PACKAGE"
val ACTION_EXTRA_OPEN_WEATHER_PROVIDER = "ACTION_EXTRA_OPEN_WEATHER_PROVIDER"

View File

@ -10,7 +10,7 @@ import java.util.Date
class Event {
var id: Int = 0
var title: String? = null
var title: String = ""
var startDate: Long = 0
var endDate: Long = 0
var calendarID: Int = 0

View File

@ -28,10 +28,6 @@ class WeatherReceiver : BroadcastReceiver() {
removeUpdates(context)
WeatherUtil.updateWeather(context)
if (SP.getBoolean(Constants.PREF_SHOW_WEATHER, true)) {
Util.showWeatherNotification(context, SP.getString(Constants.PREF_WEATHER_PROVIDER_API_KEY, "").equals(""))
}
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val i = Intent(context, WeatherReceiver::class.java)
i.action = Constants.ACTION_WEATHER_UPDATE

View File

@ -198,6 +198,13 @@ class MainActivity : AppCompatActivity() {
.commit()
Util.updateWidget(this)
updateSettings()
} else if (requestCode == Constants.EVENT_APP_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
SP.edit()
.putString(Constants.PREF_EVENT_APP_NAME, data.getStringExtra(Constants.RESULT_APP_NAME))
.putString(Constants.PREF_EVENT_APP_PACKAGE, data.getStringExtra(Constants.RESULT_APP_PACKAGE))
.commit()
Util.updateWidget(this)
updateSettings()
} else if (requestCode == Constants.WEATHER_PROVIDER_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
WeatherReceiver().setOneTimeUpdate(this)
sendBroadcast(Intent(Constants.ACTION_WEATHER_UPDATE))
@ -237,25 +244,7 @@ class MainActivity : AppCompatActivity() {
val e = CalendarUtil.getNextEvent(this)
if (e.id != 0) {
val difference = e.startDate - now.timeInMillis
if (difference > 1000 * 60) {
var time = ""
val hour = TimeUnit.MILLISECONDS.toHours(difference)
if (hour > 0) {
time = hour.toString() + getString(R.string.h_code) + " "
}
val minutes = TimeUnit.MILLISECONDS.toMinutes(difference - hour * 3600 * 1000)
if (minutes > 0) {
time += "" + minutes + getString(R.string.min_code)
}
next_event.text = String.format("%s %s %s", e.title, getString(R.string.in_code), time)
} else {
next_event.text = String.format("%s", e.title)
}
next_event.text = Util.getDifferenceText(this, e.title, now.timeInMillis, e.startDate)
if (!e.allDay) {
val startHour: String = if (SP.getString(Constants.PREF_HOUR_FORMAT, "12").equals("12")) Constants.badHourFormat.format(e.startDate) else Constants.goodHourFormat.format(e.startDate)
@ -268,7 +257,9 @@ class MainActivity : AppCompatActivity() {
val endCal = Calendar.getInstance()
endCal.timeInMillis = e.endDate
if (startCal.get(Calendar.HOUR_OF_DAY) >= endCal.get(Calendar.HOUR_OF_DAY)) {
if (startCal.get(Calendar.HOUR_OF_DAY) > endCal.get(Calendar.HOUR_OF_DAY)) {
dayDiff++
} else if (startCal.get(Calendar.HOUR_OF_DAY) == endCal.get(Calendar.HOUR_OF_DAY) && startCal.get(Calendar.MINUTE) >= endCal.get(Calendar.MINUTE)) {
dayDiff++
}
var multipleDay: String = ""
@ -455,6 +446,11 @@ class MainActivity : AppCompatActivity() {
startActivityForResult(Intent(this, ChooseApplicationActivity::class.java), Constants.WEATHER_APP_REQUEST_CODE)
}
event_app_label.text = SP.getString(Constants.PREF_EVENT_APP_NAME, getString(R.string.default_name))
action_event_app.setOnClickListener {
startActivityForResult(Intent(this, ChooseApplicationActivity::class.java), Constants.EVENT_APP_REQUEST_CODE)
}
if (!SP.getString(Constants.PREF_WEATHER_PROVIDER_API_KEY, "").equals("")) {
label_weather_provider_api_key.text = getString(R.string.settings_weather_provider_api_key_subtitle_all_set)
alert_icon.visibility = View.GONE

View File

@ -34,7 +34,9 @@ class WeatherProviderActivity : AppCompatActivity() {
val SP = PreferenceManager.getDefaultSharedPreferences(this)
action_paste.setOnClickListener {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
api_key.setText(clipboard.primaryClip.getItemAt(0).text)
if (clipboard.primaryClip != null && clipboard.primaryClip.itemCount > 0) {
api_key.setText(clipboard.primaryClip.getItemAt(0).text)
}
}
Util.collapse(button_container)

View File

@ -96,23 +96,7 @@ class TheWidget : AppWidgetProvider() {
val e = CalendarUtil.getNextEvent(context)
if (e.id != 0) {
val difference = e.startDate - now.timeInMillis
if (difference > 1000 * 60) {
var time = ""
val hour = TimeUnit.MILLISECONDS.toHours(difference)
if (hour > 0) {
time = hour.toString() + context.getString(R.string.h_code) + " "
}
val minutes = TimeUnit.MILLISECONDS.toMinutes(difference - hour * 3600 * 1000)
if (minutes > 0) {
time += "" + minutes + context.getString(R.string.min_code)
}
views.setTextViewText(R.id.next_event, String.format("%s %s %s", e.title, context.getString(R.string.in_code), time))
} else {
views.setTextViewText(R.id.next_event, String.format("%s", e.title))
}
views.setTextViewText(R.id.next_event, Util.getDifferenceText(context, e.title, now.timeInMillis, e.startDate))
if (!e.allDay) {
val startHour: String = if (SP.getString(Constants.PREF_HOUR_FORMAT, "12").equals("12")) Constants.badHourFormat.format(e.startDate) else Constants.goodHourFormat.format(e.startDate)
@ -125,7 +109,9 @@ class TheWidget : AppWidgetProvider() {
val endCal = Calendar.getInstance()
endCal.timeInMillis = e.endDate
if (startCal.get(Calendar.HOUR_OF_DAY) >= endCal.get(Calendar.HOUR_OF_DAY)) {
if (startCal.get(Calendar.HOUR_OF_DAY) > endCal.get(Calendar.HOUR_OF_DAY)) {
dayDiff++
} else if (startCal.get(Calendar.HOUR_OF_DAY) == endCal.get(Calendar.HOUR_OF_DAY) && startCal.get(Calendar.MINUTE) >= endCal.get(Calendar.MINUTE)) {
dayDiff++
}
@ -143,12 +129,8 @@ class TheWidget : AppWidgetProvider() {
views.setViewVisibility(R.id.calendar_layout, View.VISIBLE)
val uri = ContentUris.withAppendedId(Events.CONTENT_URI, e.id.toLong())
val intent = Intent(Intent.ACTION_VIEW)
.setData(uri)
intent.putExtra("beginTime", e.startDate);
intent.putExtra("endTime", e.endDate);
val pIntent = PendingIntent.getActivity(context, widgetID, intent, 0)
val pIntent = PendingIntent.getActivity(context, widgetID, Util.getEventIntent(context, e), 0)
views.setOnClickPendingIntent(R.id.main_layout, pIntent)
}
}

View File

@ -27,6 +27,7 @@ import android.util.TypedValue
import android.content.Intent
import android.content.ComponentName
import android.preference.PreferenceManager
import android.provider.CalendarContract
import android.provider.Settings
import android.util.Log
import android.view.View
@ -35,6 +36,9 @@ import android.view.animation.Transformation
import android.widget.LinearLayout
import android.widget.Toast
import com.tommasoberlose.anotherwidget.`object`.Constants
import com.tommasoberlose.anotherwidget.`object`.Event
import org.joda.time.DateTime
import java.util.concurrent.TimeUnit
/**
@ -236,6 +240,32 @@ object Util {
}
}
fun getEventIntent(context: Context, e: Event): Intent {
val SP = PreferenceManager.getDefaultSharedPreferences(context)
if (SP.getString(Constants.PREF_EVENT_APP_PACKAGE, "").equals("")) {
val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, e.id.toLong())
val intent = Intent(Intent.ACTION_VIEW)
.setData(uri)
intent.putExtra("beginTime", e.startDate);
intent.putExtra("endTime", e.endDate);
return intent
} else {
val pm: PackageManager = context.packageManager
return try {
val intent: Intent = pm.getLaunchIntentForPackage(SP.getString(Constants.PREF_CALENDAR_APP_PACKAGE, ""))
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent
} catch (ex: Exception) {
val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, e.id.toLong())
val intent = Intent(Intent.ACTION_VIEW)
.setData(uri)
intent.putExtra("beginTime", e.startDate);
intent.putExtra("endTime", e.endDate);
intent
}
}
}
fun getCapWordString(text: String): String {
return try {
val ar = text.split(" ")
@ -360,4 +390,48 @@ object Util {
fun getEmojiByUnicode(unicode: Int): String {
return String(Character.toChars(unicode))
}
fun getDifferenceText(context: Context, title: String, now: Long, start: Long): String {
val nowDate = DateTime(now)
val eventDate = DateTime(start)
val difference = start - now
if (difference < 0) {
return String.format("%s", title)
} else if (difference < 1000 * 60) {
val minutes = TimeUnit.MILLISECONDS.toMinutes(difference)
var time = ""
if (minutes > 0) {
time += "" + minutes + context.getString(R.string.min_code)
}
return String.format("%s %s %s", title, context.getString(R.string.in_code), time)
} else if (difference < 1000 * 60 * 6) {
val hour = TimeUnit.MILLISECONDS.toHours(difference)
var time = ""
if (hour > 0) {
time = if (hour > 1) {
hour.toString() + context.getString(R.string.hs_code) + " "
} else {
hour.toString() + context.getString(R.string.h_code) + " "
}
}
val minutes = TimeUnit.MILLISECONDS.toMinutes(difference - hour * 3600 * 1000)
if (minutes > 0) {
time += "" + minutes + context.getString(R.string.min_code)
}
return String.format("%s %s %s", title, context.getString(R.string.in_code), time)
} else if (eventDate.dayOfYear == nowDate.plusDays(1).dayOfYear) {
return String.format("%s %s", title, context.getString(R.string.tomorrow))
} else if (eventDate.dayOfYear == nowDate.dayOfYear) {
return String.format("%s %s", title, context.getString(R.string.today))
} else {
val days = TimeUnit.MILLISECONDS.toDays(difference)
return String.format("%s %s %s%s", title, context.getString(R.string.in_code), days, context.getString(R.string.day_char))
}
}
}

View File

@ -21,6 +21,11 @@ import android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS
import android.content.Intent
import android.location.LocationManager
import android.util.Log
import com.google.android.gms.awareness.Awareness
import com.google.android.gms.awareness.snapshot.WeatherResponse
import com.google.android.gms.awareness.state.Weather
import com.google.android.gms.tasks.OnFailureListener
import com.google.android.gms.tasks.OnSuccessListener
/**
@ -32,8 +37,12 @@ object WeatherUtil {
fun updateWeather(context: Context) {
Util.showLocationNotification(context, false)
val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
if (SP.getString(Constants.PREF_CUSTOM_LOCATION_ADD, "").equals("") || SP.getString(Constants.PREF_CUSTOM_LOCATION_LAT, "").equals("") || SP.getString(Constants.PREF_CUSTOM_LOCATION_LON, "").equals("")) {
newWeatherProvider(context)
return
/*
if (!Util.checkGrantedPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) {
return
}
@ -50,56 +59,44 @@ object WeatherUtil {
} catch (ex: Exception) {
}
if (!gpsEnabled && !networkEnabled) {
if (!gpsEnabled && !networkEnabled && SP.getBoolean(Constants.PREF_SHOW_WEATHER, true)) {
Util.showLocationNotification(context, true)
} else {
if (gpsEnabled) {
getCurrentWeather(context, locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER))
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, object : LocationListener {
override fun onLocationChanged(location: Location) {
locationManager.removeUpdates(this)
getCurrentWeather(context, location)
}
@SuppressLint("ApplySharedPref")
override fun onProviderDisabled(p0: String?) {
}
@SuppressLint("ApplySharedPref")
override fun onProviderEnabled(p0: String?) {
}
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
}
})
val gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
if (gpsLocation != null) {
getCurrentWeather(context, gpsLocation)
return
}
}
if (networkEnabled) {
getCurrentWeather(context, locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER))
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f, object : LocationListener {
override fun onLocationChanged(location: Location) {
locationManager.removeUpdates(this)
getCurrentWeather(context, location)
}
@SuppressLint("ApplySharedPref")
override fun onProviderDisabled(p0: String?) {
}
@SuppressLint("ApplySharedPref")
override fun onProviderEnabled(p0: String?) {
}
override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) {
}
})
}
}
}*/
} else {
weatherNetworkRequest(context, SP.getString(Constants.PREF_CUSTOM_LOCATION_LAT, "").toDouble(), SP.getString(Constants.PREF_CUSTOM_LOCATION_LON, "").toDouble())
}
}
@SuppressLint("ApplySharedPref")
fun newWeatherProvider(context: Context) {
if (!Util.checkGrantedPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) {
return
}
Awareness.getSnapshotClient(context).weather
.addOnSuccessListener { weatherResponse ->
val weather = weatherResponse.weather
val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
SP.edit()
.putFloat(Constants.PREF_WEATHER_TEMP, weather.getTemperature(if (SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F").equals("F")) Weather.FAHRENHEIT else Weather.CELSIUS))
.putString(Constants.PREF_WEATHER_ICON, weather.conditions[0].toString())
.putString(Constants.PREF_WEATHER_REAL_TEMP_UNIT, SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F"))
.commit()
Util.updateWidget(context)
}
}
@SuppressLint("ApplySharedPref")
fun getCurrentWeather(context: Context, location: Location?) {
if (location != null) {