diff --git a/app/build.gradle b/app/build.gradle index e42095a..6e859f1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,7 +17,7 @@ android { applicationId "com.tommasoberlose.anotherwidget" minSdkVersion 19 targetSdkVersion 26 - versionCode 27 + versionCode 28 versionName "1.3" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/app/release/output.json b/app/release/output.json index e921328..c964bde 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":27},"path":"app-release.apk","properties":{"packageId":"com.tommasoberlose.anotherwidget","split":"","minSdkVersion":"19"}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":28},"path":"app-release.apk","properties":{"packageId":"com.tommasoberlose.anotherwidget","split":"","minSdkVersion":"19"}}] \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2a7811a..65c8ddb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" + android:name=".ui.AWApplication" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> @@ -113,7 +114,7 @@ + android:exported="false"> @@ -121,10 +122,12 @@ + android:enabled="true"/> - + \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/object/Constants.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Constants.kt index ec123ed..bbc910e 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/object/Constants.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Constants.kt @@ -32,6 +32,7 @@ object Constants { val PREF_CALENDAR_ALL_DAY = "PREF_CALENDAR_ALL_DAY" val PREF_CALENDAR_FILTER = "PREF_CALENDAR_FILTER" + val PREF_EVENT_ID = "PREF_EVENT_ID" val PREF_NEXT_EVENT_ID = "PREF_NEXT_EVENT_ID" val PREF_NEXT_EVENT_NAME = "PREF_NEXT_EVENT_NAME" val PREF_NEXT_EVENT_START_DATE = "PREF_NEXT_EVENT_START_DATE" @@ -69,6 +70,8 @@ object Constants { val PREF_SECOND_ROW_INFORMATION = "PREF_SECOND_ROW_INFORMATION" val PREF_CUSTOM_FONT = "PREF_CUSTOM_FONT" val PREF_SHOW_NEXT_EVENT = "PREF_SHOW_NEXT_EVENT" + val PREF_SHOW_WIDGET_PREVIEW = "PREF_SHOW_WIDGET_PREVIEW" + val PREF_SCHEMA_VERSION = "PREF_SCHEMA_VERSION" val CUSTOM_FONT_PRODUCT_SANS = 1 diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/object/Event.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Event.kt index 0eb6744..0120edd 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/object/Event.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Event.kt @@ -10,7 +10,8 @@ import java.util.Date * Created by tommaso on 05/10/17. */ -open class Event(var id: Int = 0, +open class Event(var id: Long = 0, + var eventID: Long = 0, var title: String = "", var startDate: Long = 0, var endDate: Long = 0, diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/OpenWeatherIntentReceiver.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/OpenWeatherIntentReceiver.kt index 07de471..e8e5a1f 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/OpenWeatherIntentReceiver.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/OpenWeatherIntentReceiver.kt @@ -3,6 +3,7 @@ package com.tommasoberlose.anotherwidget.receiver import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.util.Log import com.tommasoberlose.anotherwidget.`object`.Constants import com.tommasoberlose.anotherwidget.util.Util diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/UpdatesReceiver.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/UpdatesReceiver.kt index bc6d26c..1986777 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/UpdatesReceiver.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/UpdatesReceiver.kt @@ -27,7 +27,7 @@ class UpdatesReceiver : BroadcastReceiver() { setUpdates(context) } else if (intent.action.equals(Constants.ACTION_TIME_UPDATE) || intent.action.equals("com.sec.android.widgetapp.APPWIDGET_RESIZE") || intent.action == "android.intent.action.USER_PRESENT") { val e: Event = CalendarUtil.getNextEvent(context) - if (e.id == 0 || e.endDate <= Calendar.getInstance().timeInMillis) { + if (e.id == 0.toLong() || e.endDate <= Calendar.getInstance().timeInMillis) { CalendarUtil.updateEventList(context) } else { Util.updateWidget(context) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/AWApplication.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/AWApplication.kt new file mode 100644 index 0000000..66adade --- /dev/null +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/AWApplication.kt @@ -0,0 +1,27 @@ +package com.tommasoberlose.anotherwidget.ui + +import android.annotation.SuppressLint +import android.app.Application +import android.content.SharedPreferences +import android.preference.PreferenceManager +import android.util.Log +import com.tommasoberlose.anotherwidget.`object`.Constants +import com.tommasoberlose.anotherwidget.util.MyMigration +import com.tommasoberlose.anotherwidget.util.Util + +import io.realm.Realm +import io.realm.RealmConfiguration + +class AWApplication : Application() { + @SuppressLint("ApplySharedPref") + override fun onCreate() { + super.onCreate() + Realm.init(this) + val config = RealmConfiguration.Builder() + .schemaVersion(2) + .migration(MyMigration()) + .deleteRealmIfMigrationNeeded() + .build() + Realm.setDefaultConfiguration(config) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/MainActivity.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/MainActivity.kt index 4ff744e..4536b97 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/MainActivity.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/MainActivity.kt @@ -59,6 +59,7 @@ class MainActivity : AppCompatActivity() { } } + @SuppressLint("ApplySharedPref") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -66,6 +67,8 @@ class MainActivity : AppCompatActivity() { sendBroadcast(Intent(Constants.ACTION_CALENDAR_UPDATE)) sendBroadcast(Intent(Constants.ACTION_WEATHER_UPDATE)) + val SP = PreferenceManager.getDefaultSharedPreferences(this) + if (intent.extras?.containsKey(Constants.ACTION_EXTRA_OPEN_WEATHER_PROVIDER) == true) { startActivityForResult(Intent(this, WeatherProviderActivity::class.java), Constants.WEATHER_PROVIDER_REQUEST_CODE) } @@ -98,6 +101,28 @@ class MainActivity : AppCompatActivity() { } }) + if (SP.getBoolean(Constants.PREF_SHOW_WIDGET_PREVIEW, true)) { + menuView.widget_preview_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_action_hide_preview)) + menuView.widget_preview_label.text = getString(R.string.action_hide_widget_preview) + menuView.action_toggle_widget_preview.setOnClickListener { + SP.edit() + .putBoolean(Constants.PREF_SHOW_WIDGET_PREVIEW, false) + .commit() + updateUI() + mBottomSheetDialog.dismiss() + } + } else { + menuView.widget_preview_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_action_show_preview)) + menuView.widget_preview_label.text = getString(R.string.action_show_widget_preview) + menuView.action_toggle_widget_preview.setOnClickListener { + SP.edit() + .putBoolean(Constants.PREF_SHOW_WIDGET_PREVIEW, true) + .commit() + updateUI() + mBottomSheetDialog.dismiss() + } + } + menuView.action_rate.setOnClickListener(object: View.OnClickListener { override fun onClick(p0: View?) { Util.rateApp(this@MainActivity, "https://play.google.com/store/apps/details?id=com.tommasoberlose.anotherwidget") @@ -183,16 +208,23 @@ class MainActivity : AppCompatActivity() { updateClockView() val SP = PreferenceManager.getDefaultSharedPreferences(this) - val displayMetrics = Resources.getSystem().displayMetrics - var height = Util.convertDpToPixel(120f, this).toInt() - 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() + + if (SP.getBoolean(Constants.PREF_SHOW_WIDGET_PREVIEW, true)) { + val displayMetrics = Resources.getSystem().displayMetrics + var height = Util.convertDpToPixel(120f, this).toInt() + 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() + } + if (SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) + SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) > 50) { + height += Util.convertDpToPixel(24f, this).toInt() + } + widget_bitmap.setImageBitmap(Util.getBitmapFromView(main_layout, displayMetrics.widthPixels, height - Util.convertDpToPixel(32f, this).toInt())) + widget.layoutParams.height = height + Util.convertDpToPixel(16f, this).toInt() + + widget.visibility = View.VISIBLE + } else { + widget.visibility = View.GONE } - if (SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) + SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) > 50) { - height += Util.convertDpToPixel(24f, this).toInt() - } - widget_bitmap.setImageBitmap(Util.getBitmapFromView(main_layout, displayMetrics.widthPixels, height - Util.convertDpToPixel(32f, this).toInt())) - widget.layoutParams.height = height + Util.convertDpToPixel(16f, this).toInt() } @@ -283,7 +315,7 @@ class MainActivity : AppCompatActivity() { if (calendarLayout) { val e = CalendarUtil.getNextEvent(this) - if (e.id != 0) { + if (e.id != 0.toLong()) { next_event.text = e.title if (SP.getBoolean(Constants.PREF_SHOW_NEXT_EVENT, false) && CalendarUtil.getEventsCount(this) > 1) { @@ -364,8 +396,8 @@ class MainActivity : AppCompatActivity() { second_row_icon.scaleX = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 18f second_row_icon.scaleY = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 18f - weather_icon.scaleX = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 20f - weather_icon.scaleY = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 20f + weather_icon.scaleX = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 18f + weather_icon.scaleY = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 18f empty_weather_icon.scaleX = SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) / 24f empty_weather_icon.scaleY = SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) / 24f diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/SupportDevActivity.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/SupportDevActivity.kt index c832173..3c12d63 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/SupportDevActivity.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/SupportDevActivity.kt @@ -32,62 +32,66 @@ class SupportDevActivity : AppCompatActivity(), BillingProcessor.IBillingHandler override fun onBillingInitialized() { loader.visibility = View.GONE - val isAvailable = BillingProcessor.isIabServiceAvailable(this) - val isOneTimePurchaseSupported = bp!!.isOneTimePurchaseSupported - if (isAvailable && isOneTimePurchaseSupported) { - val coffee = bp!!.getPurchaseListingDetails("donation_coffee") - val donuts = bp!!.getPurchaseListingDetails("donation_donuts") - val breakfast = bp!!.getPurchaseListingDetails("donation_breakfast") - val lunch = bp!!.getPurchaseListingDetails("donation_lunch") - val dinner = bp!!.getPurchaseListingDetails("donation_dinner") + try { + val isAvailable = BillingProcessor.isIabServiceAvailable(this) + val isOneTimePurchaseSupported = bp!!.isOneTimePurchaseSupported + if (isAvailable && isOneTimePurchaseSupported) { + val coffee = bp!!.getPurchaseListingDetails("donation_coffee") + val donuts = bp!!.getPurchaseListingDetails("donation_donuts") + val breakfast = bp!!.getPurchaseListingDetails("donation_breakfast") + val lunch = bp!!.getPurchaseListingDetails("donation_lunch") + val dinner = bp!!.getPurchaseListingDetails("donation_dinner") - if (coffee != null) { - import_donation_coffee.text = coffee.priceText - action_donation_coffee.setOnClickListener { - bp!!.purchase(this, "donation_coffee") + if (coffee != null) { + import_donation_coffee.text = coffee.priceText + action_donation_coffee.setOnClickListener { + bp!!.purchase(this, "donation_coffee") + } + } else { + action_donation_coffee.visibility = View.GONE } - } else { - action_donation_coffee.visibility = View.GONE - } - if (donuts != null) { - import_donation_donuts.text = donuts.priceText - action_donation_donuts.setOnClickListener { - bp!!.purchase(this, "donation_donuts") + if (donuts != null) { + import_donation_donuts.text = donuts.priceText + action_donation_donuts.setOnClickListener { + bp!!.purchase(this, "donation_donuts") + } + } else { + action_donation_donuts.visibility = View.GONE } - } else { - action_donation_donuts.visibility = View.GONE - } - if (breakfast != null) { - import_donation_breakfast.text = breakfast.priceText - action_donation_breakfast.setOnClickListener { - bp!!.purchase(this, "donation_breakfast") + if (breakfast != null) { + import_donation_breakfast.text = breakfast.priceText + action_donation_breakfast.setOnClickListener { + bp!!.purchase(this, "donation_breakfast") + } + } else { + action_donation_breakfast.visibility = View.GONE } - } else { - action_donation_breakfast.visibility = View.GONE - } - if (lunch != null) { - import_donation_lunch.text = lunch.priceText - action_donation_lunch.setOnClickListener { - bp!!.purchase(this, "donation_lunch") + if (lunch != null) { + import_donation_lunch.text = lunch.priceText + action_donation_lunch.setOnClickListener { + bp!!.purchase(this, "donation_lunch") + } + } else { + action_donation_lunch.visibility = View.GONE } - } else { - action_donation_lunch.visibility = View.GONE - } - if (dinner != null) { - import_donation_dinner.text = dinner.priceText - action_donation_dinner.setOnClickListener { - bp!!.purchase(this, "donation_dinner") + if (dinner != null) { + import_donation_dinner.text = dinner.priceText + action_donation_dinner.setOnClickListener { + bp!!.purchase(this, "donation_dinner") + } + } else { + action_donation_dinner.visibility = View.GONE } - } else { - action_donation_dinner.visibility = View.GONE - } - products_list.visibility = View.VISIBLE - } else { + products_list.visibility = View.VISIBLE + } else { + products_card.visibility = View.GONE + } + } catch (ignored: Exception) { products_card.visibility = View.GONE } } diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widget/TheWidget.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widget/TheWidget.kt index 9b72f48..feea9af 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widget/TheWidget.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widget/TheWidget.kt @@ -45,6 +45,7 @@ import android.text.style.StyleSpan import android.util.DisplayMetrics import android.util.TypedValue import android.widget.LinearLayout +import com.tommasoberlose.anotherwidget.receiver.OpenWeatherIntentReceiver import com.tommasoberlose.anotherwidget.ui.view.CustomTypefaceSpan import kotlinx.android.synthetic.main.the_widget.* import kotlinx.android.synthetic.main.the_widget.view.* @@ -136,6 +137,8 @@ class TheWidget : AppWidgetProvider() { views = updateLocationView(context, views, appWidgetId) views = updateClockView(context, views, appWidgetId) + views = fixViewsMargin(context, views) + appWidgetManager.updateAppWidget(appWidgetId, views) } @@ -160,7 +163,7 @@ class TheWidget : AppWidgetProvider() { if (calendarLayout) { val e = CalendarUtil.getNextEvent(context) - if (e.id != 0) { + if (e.id != 0.toLong()) { views.setTextViewText(R.id.next_event, e.title) if (SP.getBoolean(Constants.PREF_SHOW_NEXT_EVENT, false) && CalendarUtil.getEventsCount(context) > 1) { @@ -277,8 +280,9 @@ class TheWidget : AppWidgetProvider() { views.setTextViewText(R.id.temp, temp) views.setTextViewText(R.id.calendar_temp, temp) - - val weatherPIntent = PendingIntent.getBroadcast(context, widgetID, Intent(Constants.ACTION_OPEN_WEATHER_INTENT), 0) + val i = Intent(context, OpenWeatherIntentReceiver::class.java) + i.action = Constants.ACTION_OPEN_WEATHER_INTENT + val weatherPIntent = PendingIntent.getBroadcast(context, widgetID, i, 0) views.setOnClickPendingIntent(R.id.weather, weatherPIntent) views.setOnClickPendingIntent(R.id.calendar_weather, weatherPIntent) @@ -293,9 +297,7 @@ class TheWidget : AppWidgetProvider() { val SP = PreferenceManager.getDefaultSharedPreferences(context) if (!SP.getBoolean(Constants.PREF_SHOW_CLOCK, false)) { views.setViewVisibility(R.id.time, View.GONE) - views.setViewVisibility(R.id.bottom_divider, View.VISIBLE) } else { - views.setViewVisibility(R.id.bottom_divider, View.GONE) val now = Calendar.getInstance() if (SP.getString(Constants.PREF_HOUR_FORMAT, "12").equals("12")) { val textBadHour = SpannableString(Constants.badHourFormat.format(now.timeInMillis).replace(" ", "")) @@ -315,6 +317,26 @@ class TheWidget : AppWidgetProvider() { return views } + fun fixViewsMargin(context: Context, views: RemoteViews): RemoteViews { + val SP = PreferenceManager.getDefaultSharedPreferences(context) + views.setViewVisibility(R.id.bottom_divider_24, View.GONE) + views.setViewVisibility(R.id.bottom_divider_16, View.GONE) + views.setViewVisibility(R.id.bottom_divider_8, View.GONE) + val eVisible = SP.getBoolean(Constants.PREF_SHOW_EVENTS, true) && Util.checkGrantedPermission(context, Manifest.permission.READ_CALENDAR) + if (SP.getBoolean(Constants.PREF_SHOW_CLOCK, false)) { + if (eVisible) { + views.setViewVisibility(R.id.bottom_divider_8, View.VISIBLE) + } else { + views.setViewVisibility(R.id.bottom_divider_24, View.VISIBLE) + } + } else { + if (eVisible) { + views.setViewVisibility(R.id.bottom_divider_8, View.VISIBLE) + } + } + return views + } + fun updateCalendarViewByLayout(context: Context, v: View): View { val SP = PreferenceManager.getDefaultSharedPreferences(context) val now = Calendar.getInstance() @@ -332,7 +354,7 @@ class TheWidget : AppWidgetProvider() { if (calendarLayout) { val e = CalendarUtil.getNextEvent(context) - if (e.id != 0) { + if (e.id != 0.toLong()) { v.next_event.text = e.title if (SP.getBoolean(Constants.PREF_SHOW_NEXT_EVENT, false) && CalendarUtil.getEventsCount(context) > 1) { @@ -418,8 +440,8 @@ class TheWidget : AppWidgetProvider() { v.second_row_icon.scaleX = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 18f v.second_row_icon.scaleY = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 18f - v.weather_icon.scaleX = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 20f - v.weather_icon.scaleY = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 20f + v.weather_icon.scaleX = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 16f + v.weather_icon.scaleY = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) / 16f v.empty_weather_icon.scaleX = SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) / 24f v.empty_weather_icon.scaleY = SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) / 24f diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt index 79fd85b..2e67c94 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt @@ -72,7 +72,7 @@ object CalendarUtil { instance.begin = start.timeInMillis - start.timeZone.getOffset(start.timeInMillis) instance.end = end.timeInMillis - end.timeZone.getOffset(end.timeInMillis) } - eventList.add(Event(e.id.toInt(), 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 ?: "")) } } } @@ -118,8 +118,7 @@ object CalendarUtil { } fun saveEvents(context: Context, eventList: ArrayList) { - Realm.init(context) - val db = Realm.getDefaultInstance() + val db = Util.getRealInstance(context) db.executeTransaction { realm -> realm.where(Event::class.java).findAll().deleteAllFromRealm() realm.copyToRealm(eventList) @@ -128,13 +127,13 @@ object CalendarUtil { @SuppressLint("ApplySharedPref") fun resetNextEventData(context: Context) { - Realm.init(context) - val db = Realm.getDefaultInstance() + val db = Util.getRealInstance(context) db.executeTransaction { db.where(Event::class.java).findAll().deleteAllFromRealm() } val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) SP.edit() + .remove(Constants.PREF_EVENT_ID) .remove(Constants.PREF_NEXT_EVENT_ID) .remove(Constants.PREF_NEXT_EVENT_NAME) .remove(Constants.PREF_NEXT_EVENT_START_DATE) @@ -150,34 +149,20 @@ object CalendarUtil { fun saveNextEventData(context: Context, event: Event) { val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) SP.edit() - .putInt(Constants.PREF_NEXT_EVENT_ID, event.id) + .putLong(Constants.PREF_EVENT_ID, event.id) .commit() Util.updateWidget(context) } fun getNextEvent(context: Context): Event { val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - Realm.init(context) - val db = Realm.getDefaultInstance() - val nextEvent = db.where(Event::class.java).equalTo("id", SP.getInt(Constants.PREF_NEXT_EVENT_ID, 0)).findFirst() + val db = Util.getRealInstance(context) + val nextEvent = db.where(Event::class.java).equalTo("id", SP.getLong(Constants.PREF_EVENT_ID, 0)).findFirst() return if (nextEvent != null) { nextEvent } else { val eventList = db.where(Event::class.java).findAll() - if (eventList.isNotEmpty()) { - eventList.sortWith(Comparator { event: Event, event1: Event -> - if (event.allDay && event1.allDay) { - event.startDate.compareTo(event1.startDate) - } else if (event.allDay) { - 1 - } else if (event1.allDay) { - -1 - } else { - event1.startDate.compareTo(event.startDate) - } - }) - eventList.reverse() eventList[0] ?: Event() } else { Event() @@ -187,36 +172,21 @@ object CalendarUtil { @SuppressLint("ApplySharedPref") fun goToNextEvent(context: Context) { - Realm.init(context) - val db = Realm.getDefaultInstance() + val db = Util.getRealInstance(context) val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) val eventList = db.where(Event::class.java).findAll() if (eventList.isNotEmpty()) { - /* - eventList.sortWith(Comparator { event: Event, event1: Event -> - if (event.allDay && event1.allDay) { - event.startDate.compareTo(event1.startDate) - } else if (event.allDay) { - 1 - } else if (event1.allDay) { - -1 - } else { - event1.startDate.compareTo(event.startDate) - } - }) - eventList.reverse()*/ - var found = false for (e in eventList) { - if (e.id == SP.getInt(Constants.PREF_NEXT_EVENT_ID, 0)) { + if (e.id == SP.getLong(Constants.PREF_EVENT_ID, 0)) { if (eventList.indexOf(e) < eventList.size - 1) { SP.edit() - .putInt(Constants.PREF_NEXT_EVENT_ID, eventList[eventList.indexOf(e) + 1]?.id ?: 0) + .putLong(Constants.PREF_EVENT_ID, eventList[eventList.indexOf(e) + 1]?.id ?: 0) .commit() } else { SP.edit() - .putInt(Constants.PREF_NEXT_EVENT_ID, eventList[0]?.id ?: 0) + .putLong(Constants.PREF_EVENT_ID, eventList[0]?.id ?: 0) .commit() } found = true @@ -226,7 +196,7 @@ object CalendarUtil { if (!found) { SP.edit() - .putInt(Constants.PREF_NEXT_EVENT_ID, eventList[0]?.id ?: 0) + .putLong(Constants.PREF_EVENT_ID, eventList[0]?.id ?: 0) .commit() } } else { @@ -237,8 +207,7 @@ object CalendarUtil { } fun getEventsCount(context: Context): Int { - Realm.init(context) - val db = Realm.getDefaultInstance() + val db = Util.getRealInstance(context) return db.where(Event::class.java).findAll().size } } \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/CrocodileService.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/util/CrocodileService.kt index 377b7a9..4affcef 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/CrocodileService.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/CrocodileService.kt @@ -51,7 +51,7 @@ class CrocodileService : Service() { override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, "Settings") + val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, "Config") .setSmallIcon(R.drawable.ic_stat_name) .setPriority(Notification.PRIORITY_MIN) .setColor(ContextCompat.getColor(this, R.color.colorPrimary)) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/MyMigration.java b/app/src/main/java/com/tommasoberlose/anotherwidget/util/MyMigration.java new file mode 100644 index 0000000..2711af0 --- /dev/null +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/MyMigration.java @@ -0,0 +1,51 @@ +package com.tommasoberlose.anotherwidget.util; + +import android.support.annotation.NonNull; +import android.util.Log; + +import io.realm.DynamicRealm; +import io.realm.DynamicRealmObject; +import io.realm.RealmMigration; +import io.realm.RealmObjectSchema; +import io.realm.RealmSchema; + +/** + * Created by tommaso on 06/11/17. + */ + +public class MyMigration implements RealmMigration { + @Override + public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) { + + RealmSchema schema = realm.getSchema(); + if (oldVersion == 1) { + RealmObjectSchema event = schema.get("Event"); + if (event != null) { + if (!event.hasField("eventID")) { + event.addField("eventID", long.class); + } + event + .addField("id_tmp", long.class) + .transform(new RealmObjectSchema.Function() { + @Override + public void apply(@NonNull DynamicRealmObject obj) { + obj.setLong("id_tmp", obj.getInt("id")); + } + }) + .removeField("id") + .renameField("id_tmp", "id"); + } + oldVersion++; + } + } + + @Override + public int hashCode() { + return 37; + } + + @Override + public boolean equals(Object o) { + return (o instanceof MyMigration); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt index dbedead..605d9a3 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt @@ -39,6 +39,8 @@ import android.widget.RemoteViews import android.widget.Toast import com.tommasoberlose.anotherwidget.`object`.Constants import com.tommasoberlose.anotherwidget.`object`.Event +import io.realm.Realm +import io.realm.RealmConfiguration import org.joda.time.DateTime import java.util.concurrent.TimeUnit @@ -68,7 +70,7 @@ object Util { val mNotificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager; if (!Util.checkGrantedPermission(context, Manifest.permission.READ_CALENDAR)) { - val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, "Settings") + val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, "Config") .setSmallIcon(R.drawable.ic_stat_name) .setPriority(Notification.PRIORITY_MIN) .setColor(ContextCompat.getColor(context, R.color.colorPrimary)) @@ -284,7 +286,7 @@ object Util { if (SP.getString(Constants.PREF_WEATHER_APP_PACKAGE, "").equals("")) { val weatherIntent: Intent = Intent(Intent.ACTION_VIEW) weatherIntent.addCategory(Intent.CATEGORY_DEFAULT) - weatherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + weatherIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK weatherIntent.data = Uri.parse("dynact://velour/weather/ProxyActivity") weatherIntent.component = ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.velour.DynamicActivityTrampoline") return weatherIntent @@ -295,12 +297,12 @@ object Util { return try { val intent: Intent = pm.getLaunchIntentForPackage(SP.getString(Constants.PREF_WEATHER_APP_PACKAGE, "")) intent.addCategory(Intent.CATEGORY_LAUNCHER) - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK intent } catch (e: Exception) { val weatherIntent: Intent = Intent(Intent.ACTION_VIEW) weatherIntent.addCategory(Intent.CATEGORY_DEFAULT) - weatherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + weatherIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK weatherIntent.data = Uri.parse("dynact://velour/weather/ProxyActivity") weatherIntent.component = ComponentName("com.google.android.googlequicksearchbox", "com.google.android.apps.gsa.velour.DynamicActivityTrampoline") weatherIntent @@ -311,7 +313,7 @@ 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 uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, e.eventID) val intent = Intent(Intent.ACTION_VIEW) .setData(uri) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) @@ -380,7 +382,7 @@ object Util { val mNotificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager; if (show) { - val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, "Settings") + val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, "Config") .setSmallIcon(R.drawable.ic_stat_name) .setPriority(Notification.PRIORITY_MIN) .setColor(ContextCompat.getColor(context, R.color.colorPrimary)) @@ -402,7 +404,7 @@ object Util { val mNotificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager; if (show) { - val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, "Settings") + val mBuilder: NotificationCompat.Builder = NotificationCompat.Builder(context, "Config") .setSmallIcon(R.drawable.ic_stat_name) .setColor(ContextCompat.getColor(context, R.color.colorPrimary)) .setContentTitle(context.getString(R.string.settings_weather_provider_api_key_title)) @@ -566,7 +568,7 @@ object Util { @SuppressLint("ApplySharedPref") fun updateSettingsByDefault(context: Context) { try { - context.startService(Intent(context, CrocodileService::class.java)) + // context.startService(Intent(context, CrocodileService::class.java)) } catch (e: Exception) { } @@ -584,6 +586,10 @@ object Util { editor.commit() } + fun getRealInstance(context: Context): Realm { + return Realm.getDefaultInstance() + } + fun getNextAlarm(context: Context): String? { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val SP = PreferenceManager.getDefaultSharedPreferences(context) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt index a00513d..75ca5cf 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt @@ -188,15 +188,10 @@ object WeatherUtil { icon = "82" } - val uiManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager - return when { - uiManager.nightMode == UiModeManager.MODE_NIGHT_YES -> icon + "n" - uiManager.nightMode == UiModeManager.MODE_NIGHT_NO -> icon + "d" - else -> return if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 19 || Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 7) { - icon + "n" - } else { - icon + "d" - } + return if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 19 || Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 7) { + icon + "n" + } else { + icon + "d" } } } diff --git a/app/src/main/res/drawable-hdpi/ic_action_hide_preview.png b/app/src/main/res/drawable-hdpi/ic_action_hide_preview.png new file mode 100644 index 0000000..0890800 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_action_hide_preview.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_action_show_preview.png b/app/src/main/res/drawable-hdpi/ic_action_show_preview.png new file mode 100644 index 0000000..4725568 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_action_show_preview.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_action_hide_preview.png b/app/src/main/res/drawable-mdpi/ic_action_hide_preview.png new file mode 100644 index 0000000..9fbd1f5 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_action_hide_preview.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_action_show_preview.png b/app/src/main/res/drawable-mdpi/ic_action_show_preview.png new file mode 100644 index 0000000..1854e96 Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_action_show_preview.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_action_hide_preview.png b/app/src/main/res/drawable-xhdpi/ic_action_hide_preview.png new file mode 100644 index 0000000..940f65d Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_action_hide_preview.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_action_show_preview.png b/app/src/main/res/drawable-xhdpi/ic_action_show_preview.png new file mode 100644 index 0000000..fe7dbeb Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_action_show_preview.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_action_hide_preview.png b/app/src/main/res/drawable-xxhdpi/ic_action_hide_preview.png new file mode 100644 index 0000000..1073dab Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_action_hide_preview.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_action_show_preview.png b/app/src/main/res/drawable-xxhdpi/ic_action_show_preview.png new file mode 100644 index 0000000..2563a5b Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_action_show_preview.png differ diff --git a/app/src/main/res/layout/main_menu_layout.xml b/app/src/main/res/layout/main_menu_layout.xml index 072fa40..bd3ac27 100644 --- a/app/src/main/res/layout/main_menu_layout.xml +++ b/app/src/main/res/layout/main_menu_layout.xml @@ -36,6 +36,35 @@ android:paddingRight="16dp" android:textColor="@android:color/black" /> + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values-it-rIT/strings.xml b/app/src/main/res/values-it-rIT/strings.xml index 2dfbda3..dd4d653 100644 --- a/app/src/main/res/values-it-rIT/strings.xml +++ b/app/src/main/res/values-it-rIT/strings.xml @@ -151,4 +151,6 @@ Una Pranzo Veloce Another Widget è attivo AW è attivo in background + Mostra Anteprima Widget + Nascondi Anteprima Widget \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8611ef1..a1db4aa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -156,4 +156,6 @@ A Quick Lunch Another Widget is Running AW is running in the background + Show Widget Preview + Hide Widget Preview diff --git a/app/src/main/res/xml-v21/the_widget_info.xml b/app/src/main/res/xml-v21/the_widget_info.xml index 9c71ab6..b7bb16b 100644 --- a/app/src/main/res/xml-v21/the_widget_info.xml +++ b/app/src/main/res/xml-v21/the_widget_info.xml @@ -8,6 +8,6 @@ android:minResizeHeight="60dp" android:minResizeWidth="300dp" android:previewImage="@drawable/widget_preview" - android:resizeMode="vertical" + android:resizeMode="vertical|horizontal" android:updatePeriodMillis="60000" android:widgetCategory="home_screen" /> \ No newline at end of file diff --git a/app/src/main/res/xml/the_widget_info.xml b/app/src/main/res/xml/the_widget_info.xml index 219c9a1..2104f8b 100644 --- a/app/src/main/res/xml/the_widget_info.xml +++ b/app/src/main/res/xml/the_widget_info.xml @@ -7,6 +7,6 @@ android:minResizeHeight="60dp" android:minResizeWidth="300dp" android:previewImage="@drawable/widget_preview" - android:resizeMode="vertical" + android:resizeMode="vertical|horizontal" android:updatePeriodMillis="60000" android:widgetCategory="home_screen" /> \ No newline at end of file