Bug fixes, move clock format settings, fix network provider for weather updates and add notification when location is turned off with google awareness as provider

This commit is contained in:
Tommaso Berlose 2017-11-09 18:15:52 +01:00
parent 0a289d82b2
commit 2757f7f66f
16 changed files with 170 additions and 100 deletions

View File

@ -17,7 +17,7 @@ android {
applicationId "com.tommasoberlose.anotherwidget" applicationId "com.tommasoberlose.anotherwidget"
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 26 targetSdkVersion 26
versionCode 28 versionCode 29
versionName "1.3" versionName "1.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }

View File

@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":28},"path":"app-release.apk","properties":{"packageId":"com.tommasoberlose.anotherwidget","split":"","minSdkVersion":"19"}}] [{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":29},"path":"app-release.apk","properties":{"packageId":"com.tommasoberlose.anotherwidget","split":"","minSdkVersion":"19"}}]

View File

@ -83,6 +83,7 @@
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="com.tommasoberlose.anotherwidget.action.ACTION_WEATHER_UPDATE" /> <action android:name="com.tommasoberlose.anotherwidget.action.ACTION_WEATHER_UPDATE" />
<action android:name="android.location.PROVIDERS_CHANGED" /> <action android:name="android.location.PROVIDERS_CHANGED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
<intent-filter> <intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" /> <action android:name="android.intent.action.PACKAGE_REPLACED" />

View File

@ -71,7 +71,6 @@ object Constants {
val PREF_CUSTOM_FONT = "PREF_CUSTOM_FONT" val PREF_CUSTOM_FONT = "PREF_CUSTOM_FONT"
val PREF_SHOW_NEXT_EVENT = "PREF_SHOW_NEXT_EVENT" val PREF_SHOW_NEXT_EVENT = "PREF_SHOW_NEXT_EVENT"
val PREF_SHOW_WIDGET_PREVIEW = "PREF_SHOW_WIDGET_PREVIEW" val PREF_SHOW_WIDGET_PREVIEW = "PREF_SHOW_WIDGET_PREVIEW"
val PREF_SCHEMA_VERSION = "PREF_SCHEMA_VERSION"
val CUSTOM_FONT_PRODUCT_SANS = 1 val CUSTOM_FONT_PRODUCT_SANS = 1

View File

@ -12,7 +12,11 @@ class OpenWeatherIntentReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Constants.ACTION_OPEN_WEATHER_INTENT) { if (intent.action == Constants.ACTION_OPEN_WEATHER_INTENT) {
context.sendBroadcast(Intent(Constants.ACTION_WEATHER_UPDATE)) context.sendBroadcast(Intent(Constants.ACTION_WEATHER_UPDATE))
context.startActivity(Util.getWeatherIntent(context)) try {
context.startActivity(Util.getWeatherIntent(context))
} catch (e: Exception) {
context.applicationContext.startActivity(Util.getWeatherIntent(context.applicationContext))
}
} }
} }
} }

View File

@ -14,6 +14,9 @@ import com.tommasoberlose.anotherwidget.util.CalendarUtil
import com.tommasoberlose.anotherwidget.util.Util import com.tommasoberlose.anotherwidget.util.Util
import com.tommasoberlose.anotherwidget.util.WeatherUtil import com.tommasoberlose.anotherwidget.util.WeatherUtil
import java.util.* import java.util.*
import android.widget.Toast
class WeatherReceiver : BroadcastReceiver() { class WeatherReceiver : BroadcastReceiver() {
@ -22,6 +25,8 @@ class WeatherReceiver : BroadcastReceiver() {
setUpdates(context) setUpdates(context)
} else if (intent.action.equals(Constants.ACTION_WEATHER_UPDATE) || intent.action.equals("android.location.PROVIDERS_CHANGED")) { } else if (intent.action.equals(Constants.ACTION_WEATHER_UPDATE) || intent.action.equals("android.location.PROVIDERS_CHANGED")) {
WeatherUtil.updateWeather(context) WeatherUtil.updateWeather(context)
} else if (intent.action == "android.location.PROVIDERS_CHANGED") {
Util.showWeatherErrorNotification(context)
} }
} }

View File

@ -211,6 +211,10 @@ class MainActivity : AppCompatActivity() {
if (SP.getBoolean(Constants.PREF_SHOW_WIDGET_PREVIEW, true)) { if (SP.getBoolean(Constants.PREF_SHOW_WIDGET_PREVIEW, true)) {
val displayMetrics = Resources.getSystem().displayMetrics val displayMetrics = Resources.getSystem().displayMetrics
var width = displayMetrics.widthPixels
if (width <= 0) {
width = Util.convertDpToPixel(300f, this).toInt()
}
var height = Util.convertDpToPixel(120f, this).toInt() var height = Util.convertDpToPixel(120f, this).toInt()
if (SP.getBoolean(Constants.PREF_SHOW_CLOCK, false)) { if (SP.getBoolean(Constants.PREF_SHOW_CLOCK, false)) {
height += Util.convertSpToPixels(SP.getFloat(Constants.PREF_TEXT_CLOCK_SIZE, 90f), this).toInt() + Util.convertDpToPixel(8f, this).toInt() height += Util.convertSpToPixels(SP.getFloat(Constants.PREF_TEXT_CLOCK_SIZE, 90f), this).toInt() + Util.convertDpToPixel(8f, this).toInt()
@ -218,7 +222,7 @@ class MainActivity : AppCompatActivity() {
if (SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) + SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) > 50) { if (SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) + SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) > 50) {
height += Util.convertDpToPixel(24f, this).toInt() height += Util.convertDpToPixel(24f, this).toInt()
} }
widget_bitmap.setImageBitmap(Util.getBitmapFromView(main_layout, displayMetrics.widthPixels, height - Util.convertDpToPixel(32f, this).toInt())) widget_bitmap.setImageBitmap(Util.getBitmapFromView(main_layout, width, height - Util.convertDpToPixel(32f, this).toInt()))
widget.layoutParams.height = height + Util.convertDpToPixel(16f, this).toInt() widget.layoutParams.height = height + Util.convertDpToPixel(16f, this).toInt()
widget.visibility = View.VISIBLE widget.visibility = View.VISIBLE
@ -649,7 +653,7 @@ class MainActivity : AppCompatActivity() {
action_clock_text_size.setOnClickListener { action_clock_text_size.setOnClickListener {
var fontSize = SP.getFloat(Constants.PREF_TEXT_CLOCK_SIZE, 90f) + 5 var fontSize = SP.getFloat(Constants.PREF_TEXT_CLOCK_SIZE, 90f) + 5
if (fontSize > 110) { if (fontSize > 110) {
fontSize = 70f fontSize = 50f
} }
SP.edit().putFloat(Constants.PREF_TEXT_CLOCK_SIZE, fontSize).commit() SP.edit().putFloat(Constants.PREF_TEXT_CLOCK_SIZE, fontSize).commit()
Util.updateWidget(this) Util.updateWidget(this)
@ -688,25 +692,19 @@ class MainActivity : AppCompatActivity() {
updateSettings() updateSettings()
} }
label_weather_refresh_period.text = getString(Util.getRefreshPeriodString(SP.getInt(Constants.PREF_WEATHER_REFRESH_PERIOD, 1)))
if (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) == Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) { action_weather_refresh_period.setOnClickListener {
action_weather_refresh_period.visibility = View.GONE SP.edit().putInt(Constants.PREF_WEATHER_REFRESH_PERIOD, when (SP.getInt(Constants.PREF_WEATHER_REFRESH_PERIOD, 1)) {
} else { 0 -> 1
label_weather_refresh_period.text = getString(Util.getRefreshPeriodString(SP.getInt(Constants.PREF_WEATHER_REFRESH_PERIOD, 1))) 1 -> 2
action_weather_refresh_period.setOnClickListener { 2 -> 3
SP.edit().putInt(Constants.PREF_WEATHER_REFRESH_PERIOD, when (SP.getInt(Constants.PREF_WEATHER_REFRESH_PERIOD, 1)) { 3 -> 4
0 -> 1 4 -> 5
1 -> 2 5 -> 0
2 -> 3 else -> 1
3 -> 4 }).commit()
4 -> 5 updateSettings()
5 -> 0 WeatherReceiver().setUpdates(this@MainActivity)
else -> 1
}).commit()
updateSettings()
WeatherReceiver().setUpdates(this@MainActivity)
}
action_weather_refresh_period.visibility = View.VISIBLE
} }
show_until_label.text = getString(Util.getShowUntilString(SP.getInt(Constants.PREF_SHOW_UNTIL, 1))) show_until_label.text = getString(Util.getShowUntilString(SP.getInt(Constants.PREF_SHOW_UNTIL, 1)))

View File

@ -12,7 +12,7 @@ import com.tommasoberlose.anotherwidget.util.Util
import kotlinx.android.synthetic.main.activity_support_dev.* import kotlinx.android.synthetic.main.activity_support_dev.*
class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler { class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler {
internal var bp: BillingProcessor? = null internal lateinit var bp: BillingProcessor
internal val BILLING_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAox5CcxuoLJ6CmNS7s6lVQzJ253njKKGF8MoQ/gQ5gEw2Fr03fBvtHpiVMpnjhNLw5NMeIpzRvkVqeQ7BfkC7c0BLCJUqf/fFA11ArQe8na6QKt5O4d+v4sbHtP7mm3GQNPOBaqRzcpFZaiAbfk6mnalo+tzM47GXrQFt5bNSrMctCs7bbChqJfH2cyMW0F8DHWEEeO5xElBmH3lh4FVpwIUTPYJIV3n0yhE3qqRA0WXkDej66g/uAt/rebmMZLmwNwIive5cObU4o41YyKRv2wSAicrv3W40LftzXAOOordIbmzDFN8ksh3VrnESqwCDGG97nZVbPG/+3LD0xHWiRwIDAQAB" internal val BILLING_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAox5CcxuoLJ6CmNS7s6lVQzJ253njKKGF8MoQ/gQ5gEw2Fr03fBvtHpiVMpnjhNLw5NMeIpzRvkVqeQ7BfkC7c0BLCJUqf/fFA11ArQe8na6QKt5O4d+v4sbHtP7mm3GQNPOBaqRzcpFZaiAbfk6mnalo+tzM47GXrQFt5bNSrMctCs7bbChqJfH2cyMW0F8DHWEEeO5xElBmH3lh4FVpwIUTPYJIV3n0yhE3qqRA0WXkDej66g/uAt/rebmMZLmwNwIive5cObU4o41YyKRv2wSAicrv3W40LftzXAOOordIbmzDFN8ksh3VrnESqwCDGG97nZVbPG/+3LD0xHWiRwIDAQAB"
@ -34,18 +34,18 @@ class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler
loader.visibility = View.GONE loader.visibility = View.GONE
try { try {
val isAvailable = BillingProcessor.isIabServiceAvailable(this) val isAvailable = BillingProcessor.isIabServiceAvailable(this)
val isOneTimePurchaseSupported = bp!!.isOneTimePurchaseSupported val isOneTimePurchaseSupported = bp.isOneTimePurchaseSupported
if (isAvailable && isOneTimePurchaseSupported) { if (isAvailable && isOneTimePurchaseSupported) {
val coffee = bp!!.getPurchaseListingDetails("donation_coffee") val coffee = bp.getPurchaseListingDetails("donation_coffee")
val donuts = bp!!.getPurchaseListingDetails("donation_donuts") val donuts = bp.getPurchaseListingDetails("donation_donuts")
val breakfast = bp!!.getPurchaseListingDetails("donation_breakfast") val breakfast = bp.getPurchaseListingDetails("donation_breakfast")
val lunch = bp!!.getPurchaseListingDetails("donation_lunch") val lunch = bp.getPurchaseListingDetails("donation_lunch")
val dinner = bp!!.getPurchaseListingDetails("donation_dinner") val dinner = bp.getPurchaseListingDetails("donation_dinner")
if (coffee != null) { if (coffee != null) {
import_donation_coffee.text = coffee.priceText import_donation_coffee.text = coffee.priceText
action_donation_coffee.setOnClickListener { action_donation_coffee.setOnClickListener {
bp!!.purchase(this, "donation_coffee") bp.purchase(this, "donation_coffee")
} }
} else { } else {
action_donation_coffee.visibility = View.GONE action_donation_coffee.visibility = View.GONE
@ -54,7 +54,7 @@ class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler
if (donuts != null) { if (donuts != null) {
import_donation_donuts.text = donuts.priceText import_donation_donuts.text = donuts.priceText
action_donation_donuts.setOnClickListener { action_donation_donuts.setOnClickListener {
bp!!.purchase(this, "donation_donuts") bp.purchase(this, "donation_donuts")
} }
} else { } else {
action_donation_donuts.visibility = View.GONE action_donation_donuts.visibility = View.GONE
@ -63,7 +63,7 @@ class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler
if (breakfast != null) { if (breakfast != null) {
import_donation_breakfast.text = breakfast.priceText import_donation_breakfast.text = breakfast.priceText
action_donation_breakfast.setOnClickListener { action_donation_breakfast.setOnClickListener {
bp!!.purchase(this, "donation_breakfast") bp.purchase(this, "donation_breakfast")
} }
} else { } else {
action_donation_breakfast.visibility = View.GONE action_donation_breakfast.visibility = View.GONE
@ -72,7 +72,7 @@ class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler
if (lunch != null) { if (lunch != null) {
import_donation_lunch.text = lunch.priceText import_donation_lunch.text = lunch.priceText
action_donation_lunch.setOnClickListener { action_donation_lunch.setOnClickListener {
bp!!.purchase(this, "donation_lunch") bp.purchase(this, "donation_lunch")
} }
} else { } else {
action_donation_lunch.visibility = View.GONE action_donation_lunch.visibility = View.GONE
@ -81,7 +81,7 @@ class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler
if (dinner != null) { if (dinner != null) {
import_donation_dinner.text = dinner.priceText import_donation_dinner.text = dinner.priceText
action_donation_dinner.setOnClickListener { action_donation_dinner.setOnClickListener {
bp!!.purchase(this, "donation_dinner") bp.purchase(this, "donation_dinner")
} }
} else { } else {
action_donation_dinner.visibility = View.GONE action_donation_dinner.visibility = View.GONE
@ -101,7 +101,7 @@ class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler
override fun onProductPurchased(productId: String, details: TransactionDetails?) { override fun onProductPurchased(productId: String, details: TransactionDetails?) {
Toast.makeText(this, R.string.thanks, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.thanks, Toast.LENGTH_SHORT).show()
bp!!.consumePurchase(productId) bp.consumePurchase(productId)
} }
override fun onBillingError(errorCode: Int, error: Throwable?) { override fun onBillingError(errorCode: Int, error: Throwable?) {
@ -109,15 +109,13 @@ class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
if (!bp!!.handleActivityResult(requestCode, resultCode, data)) { if (!bp.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data) super.onActivityResult(requestCode, resultCode, data)
} }
} }
public override fun onDestroy() { public override fun onDestroy() {
if (bp != null) { bp.release()
bp!!.release()
}
super.onDestroy() super.onDestroy()
} }
} }

View File

@ -73,7 +73,6 @@ class TheWidget : AppWidgetProvider() {
override fun onEnabled(context: Context) { override fun onEnabled(context: Context) {
UpdatesReceiver().setUpdates(context) UpdatesReceiver().setUpdates(context)
WeatherReceiver().setUpdates(context) WeatherReceiver().setUpdates(context)
Util.showNotification(context)
} }
override fun onDisabled(context: Context) { override fun onDisabled(context: Context) {

View File

@ -62,18 +62,20 @@ object CalendarUtil {
if (data != null) { if (data != null) {
val instances = data.list val instances = data.list
for (instance in instances) { for (instance in instances) {
val e = provider.getEvent(instance.eventId) try {
if (e != null && instance.begin <= limit.timeInMillis && (SP.getBoolean(Constants.PREF_CALENDAR_ALL_DAY, false) || !e.allDay) && !(SP.getString(Constants.PREF_CALENDAR_FILTER, "").contains(" " + e.calendarId + ",")) && (SP.getBoolean(Constants.PREF_SHOW_DECLINED_EVENTS, true) || !e.selfAttendeeStatus.equals(CalendarContract.Attendees.ATTENDEE_STATUS_DECLINED))) { val e = provider.getEvent(instance.eventId)
if (e.allDay) { if (e != null && instance.begin <= limit.timeInMillis && (SP.getBoolean(Constants.PREF_CALENDAR_ALL_DAY, false) || !e.allDay) && !(SP.getString(Constants.PREF_CALENDAR_FILTER, "").contains(" " + e.calendarId + ",")) && (SP.getBoolean(Constants.PREF_SHOW_DECLINED_EVENTS, true) || !e.selfAttendeeStatus.equals(CalendarContract.Attendees.ATTENDEE_STATUS_DECLINED))) {
val start = Calendar.getInstance() if (e.allDay) {
start.timeInMillis = instance.begin val start = Calendar.getInstance()
val end = Calendar.getInstance() start.timeInMillis = instance.begin
end.timeInMillis = instance.end val end = Calendar.getInstance()
instance.begin = start.timeInMillis - start.timeZone.getOffset(start.timeInMillis) end.timeInMillis = instance.end
instance.end = end.timeInMillis - end.timeZone.getOffset(end.timeInMillis) instance.begin = start.timeInMillis - start.timeZone.getOffset(start.timeInMillis)
instance.end = end.timeInMillis - end.timeZone.getOffset(end.timeInMillis)
}
eventList.add(Event(instance.id, e.id, e.title ?: "", instance.begin, instance.end, e.calendarId.toInt(), e.allDay, e.eventLocation ?: ""))
} }
eventList.add(Event(instance.id, e.id, e.title, instance.begin, instance.end, e.calendarId.toInt(), e.allDay, e.eventLocation ?: "")) } catch (ignored: Exception) {}
}
} }
} }

View File

@ -24,6 +24,7 @@ import android.support.annotation.StringRes
import android.util.TypedValue import android.util.TypedValue
import android.content.Intent import android.content.Intent
import android.content.ComponentName import android.content.ComponentName
import android.location.LocationManager
import android.os.Build import android.os.Build
import android.preference.PreferenceManager import android.preference.PreferenceManager
import android.provider.AlarmClock import android.provider.AlarmClock
@ -88,6 +89,28 @@ object Util {
} }
fun showWeatherErrorNotification(context: Context) {
val mNotificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val SP = PreferenceManager.getDefaultSharedPreferences(context)
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) == Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) {
val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, "Error")
.setSmallIcon(R.drawable.ic_stat_name)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setLargeIcon(BitmapFactory.decodeResource(context.resources, R.mipmap.ic_launcher_round))
.setContentTitle(context.getString(R.string.notification_gps_title))
.setContentText(context.getString(R.string.notification_gps_subtitle))
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
val pi: PendingIntent = PendingIntent.getActivity(context, 50, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
mNotificationManager.notify(10, mBuilder.build());
} else {
mNotificationManager.cancel(10)
}
}
fun openURI(context: Context, url: String) { fun openURI(context: Context, url: String) {
try { try {
val builder: CustomTabsIntent.Builder = CustomTabsIntent.Builder() val builder: CustomTabsIntent.Builder = CustomTabsIntent.Builder()
@ -587,28 +610,33 @@ object Util {
} }
fun getRealInstance(context: Context): Realm { fun getRealInstance(context: Context): Realm {
Realm.init(context)
return Realm.getDefaultInstance() return Realm.getDefaultInstance()
} }
fun getNextAlarm(context: Context): String? { fun getNextAlarm(context: Context): String? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { try {
val SP = PreferenceManager.getDefaultSharedPreferences(context) return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager val SP = PreferenceManager.getDefaultSharedPreferences(context)
val alarm = am.nextAlarmClock val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (alarm != null) { val alarm = am.nextAlarmClock
val time = am.nextAlarmClock.triggerTime if (alarm != null) {
if (SP.getString(Constants.PREF_HOUR_FORMAT, "12") == "12") Constants.badHourFormat.format(time) else Constants.goodHourFormat.format(time) val time = am.nextAlarmClock.triggerTime
if (SP.getString(Constants.PREF_HOUR_FORMAT, "12") == "12") Constants.badHourFormat.format(time) else Constants.goodHourFormat.format(time)
} else {
null
}
} else { } else {
null val time = Settings.System.getString(context.contentResolver,
} Settings.System.NEXT_ALARM_FORMATTED)
} else { return if (time != "") {
val time = Settings.System.getString(context.contentResolver, time
Settings.System.NEXT_ALARM_FORMATTED) } else {
return if (time != "") { null
time }
} else {
null
} }
} catch (ignored: Exception) {
return null
} }
} }
} }

View File

@ -4,6 +4,7 @@ import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.UiModeManager import android.app.UiModeManager
import android.content.Context import android.content.Context
import android.content.Context.LOCATION_SERVICE
import android.content.SharedPreferences import android.content.SharedPreferences
import android.location.* import android.location.*
import android.os.Bundle import android.os.Bundle
@ -44,6 +45,7 @@ object WeatherUtil {
fun updateWeather(context: Context) { fun updateWeather(context: Context) {
Util.showLocationNotification(context, false) Util.showLocationNotification(context, false)
val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
Util.showWeatherErrorNotification(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("")) { 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("")) {
if (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) == Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) { if (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) == Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) {
@ -60,6 +62,25 @@ object WeatherUtil {
.setResultCallback({ locationResult -> .setResultCallback({ locationResult ->
if (locationResult.status.isSuccess) { if (locationResult.status.isSuccess) {
getCurrentWeather(context, locationResult.location) getCurrentWeather(context, locationResult.location)
} else {
val locationManager = context.getSystemService(LOCATION_SERVICE) as LocationManager
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
val gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
if (gpsLocation != null) {
getCurrentWeather(context, gpsLocation)
} else {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
val networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
if (networkLocation != null) {
getCurrentWeather(context, networkLocation)
} else {
getWeatherByDefaultLocation(context)
}
} else {
getWeatherByDefaultLocation(context)
}
}
}
} }
}) })
} }
@ -75,7 +96,7 @@ object WeatherUtil {
} }
val mGoogleApiClient = GoogleApiClient.Builder(context) val mGoogleApiClient = GoogleApiClient.Builder(context)
.addApi(Awareness.API) .addApi(Awareness.API)
.build(); .build()
mGoogleApiClient.connect() mGoogleApiClient.connect()
Awareness.SnapshotApi.getWeather(mGoogleApiClient) Awareness.SnapshotApi.getWeather(mGoogleApiClient)
@ -95,6 +116,13 @@ object WeatherUtil {
} }
fun getWeatherByDefaultLocation(context: Context) {
val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
if (!SP.getString(Constants.PREF_CUSTOM_LOCATION_LAT, "").equals("") && !SP.getString(Constants.PREF_CUSTOM_LOCATION_LON, "").equals("")) {
weatherNetworkRequest(context, SP.getString(Constants.PREF_CUSTOM_LOCATION_LAT, "").toDouble(), SP.getString(Constants.PREF_CUSTOM_LOCATION_LON, "").toDouble())
}
}
@SuppressLint("ApplySharedPref") @SuppressLint("ApplySharedPref")
fun getCurrentWeather(context: Context, location: Location?) { fun getCurrentWeather(context: Context, location: Location?) {
if (location != null) { if (location != null) {
@ -104,6 +132,11 @@ object WeatherUtil {
fun weatherNetworkRequest(context: Context, latitude: Double, longitude: Double) { fun weatherNetworkRequest(context: Context, latitude: Double, longitude: Double) {
val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
SP.edit()
.putString(Constants.PREF_CUSTOM_LOCATION_LAT, latitude.toString())
.putString(Constants.PREF_CUSTOM_LOCATION_LON, longitude.toString())
.apply()
if (!SP.getString(when (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS)) { if (!SP.getString(when (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS)) {
Constants.WEATHER_PROVIDER_OPEN_WEATHER -> Constants.PREF_OPEN_WEATHER_API_KEY Constants.WEATHER_PROVIDER_OPEN_WEATHER -> Constants.PREF_OPEN_WEATHER_API_KEY
else -> Constants.PREF_OPEN_WEATHER_API_KEY else -> Constants.PREF_OPEN_WEATHER_API_KEY

View File

@ -281,6 +281,30 @@
android:id="@+id/clock_text_size_label" android:id="@+id/clock_text_size_label"
style="@style/AnotherWidget.Settings.Subtitle"/> style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical"
android:id="@+id/action_hour_format"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Settings.Title"
android:text="@string/settings_hour_format_title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hour_format_label"
style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -373,30 +397,6 @@
android:id="@+id/date_format_label" android:id="@+id/date_format_label"
style="@style/AnotherWidget.Settings.Subtitle"/> style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical"
android:id="@+id/action_hour_format"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/AnotherWidget.Settings.Title"
android:text="@string/settings_hour_format_title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hour_format_label"
style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@ -9,7 +9,6 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:gravity="center" android:gravity="center"
android:layout_marginTop="-16dp"
android:lineSpacingMultiplier="1" android:lineSpacingMultiplier="1"
android:lineSpacingExtra="0dp" android:lineSpacingExtra="0dp"
android:visibility="gone" android:visibility="gone"

View File

@ -71,8 +71,8 @@
<string name="settings_weather_provider_api_key_title">"Provider del Meteo "</string> <string name="settings_weather_provider_api_key_title">"Provider del Meteo "</string>
<string name="settings_weather_provider_api_key_subtitle_all_set">Il provider meteo è configurato correttamente</string> <string name="settings_weather_provider_api_key_subtitle_all_set">Il provider meteo è configurato correttamente</string>
<string name="settings_weather_provider_api_key_subtitle_not_set">Il provider meteo deve essere configurato</string> <string name="settings_weather_provider_api_key_subtitle_not_set">Il provider meteo deve essere configurato</string>
<string name="notification_gps_title">Localizzazione disattivata</string> <string name="notification_gps_title">Riattiva la localizzazione</string>
<string name="notification_gps_subtitle">Riaccendi la localizzazione per avere il meteo aggiornato.</string> <string name="notification_gps_subtitle">Google Awareness ha bisogno del GPS attivo per funzionare.</string>
<string name="action_feedback">Feedback</string> <string name="action_feedback">Feedback</string>
<string name="feedback_title">AW Feedback</string> <string name="feedback_title">AW Feedback</string>
<string name="feedback_chooser_title">Invia email…</string> <string name="feedback_chooser_title">Invia email…</string>
@ -153,4 +153,6 @@
<string name="background_service_subtitle">AW è attivo in background</string> <string name="background_service_subtitle">AW è attivo in background</string>
<string name="action_show_widget_preview">Mostra Anteprima Widget</string> <string name="action_show_widget_preview">Mostra Anteprima Widget</string>
<string name="action_hide_widget_preview">Nascondi Anteprima Widget</string> <string name="action_hide_widget_preview">Nascondi Anteprima Widget</string>
<string name="error_widget_notification_title">GPS Disattivato</string>
<string name="error_widget_notification_subtitle">Riattiva il GPS così che Another Widget possa aggiornare il meteo con le API di Google Awareness.</string>
</resources> </resources>

View File

@ -73,8 +73,8 @@
<string name="settings_weather_provider_api_key_title">Weather Provider</string> <string name="settings_weather_provider_api_key_title">Weather Provider</string>
<string name="settings_weather_provider_api_key_subtitle_all_set">The weather provider is configured correctly</string> <string name="settings_weather_provider_api_key_subtitle_all_set">The weather provider is configured correctly</string>
<string name="settings_weather_provider_api_key_subtitle_not_set">The weather provider must be configured</string> <string name="settings_weather_provider_api_key_subtitle_not_set">The weather provider must be configured</string>
<string name="notification_gps_title">Location is turned off</string> <string name="notification_gps_title">Turn location on</string>
<string name="notification_gps_subtitle">Turn on the localization to get the updated weather.</string> <string name="notification_gps_subtitle">Google Awareness needs active GPS to work.</string>
<string name="action_feedback">Feedback</string> <string name="action_feedback">Feedback</string>
<string name="feedback_title">AW Feedback</string> <string name="feedback_title">AW Feedback</string>
<string name="feedback_chooser_title">Send email…</string> <string name="feedback_chooser_title">Send email…</string>
@ -158,4 +158,6 @@
<string name="background_service_subtitle">AW is running in the background</string> <string name="background_service_subtitle">AW is running in the background</string>
<string name="action_show_widget_preview">Show Widget Preview</string> <string name="action_show_widget_preview">Show Widget Preview</string>
<string name="action_hide_widget_preview">Hide Widget Preview</string> <string name="action_hide_widget_preview">Hide Widget Preview</string>
<string name="error_widget_notification_title">Location is turned off</string>
<string name="error_widget_notification_subtitle">Turn Location back on so Another Widget can update weather informations with Google Awareness API.</string>
</resources> </resources>