From b0559e9a1507863d133eff1f1905ab6605c129b4 Mon Sep 17 00:00:00 2001 From: Tommaso Berlose Date: Fri, 20 Oct 2017 20:06:59 +0200 Subject: [PATCH] THE Update --- app/build.gradle | 5 +- app/release/output.json | 2 +- .../anotherwidget/object/CalendarSelector.kt | 6 +- .../anotherwidget/object/Constants.kt | 5 + .../anotherwidget/object/Event.kt | 5 +- .../anotherwidget/ui/activity/MainActivity.kt | 158 ++++++++++++---- .../anotherwidget/ui/widget/TheWidget.kt | 90 ++++++--- .../anotherwidget/util/CalendarUtil.kt | 15 +- .../tommasoberlose/anotherwidget/util/Util.kt | 61 +++++- .../anotherwidget/util/WeatherUtil.kt | 59 +++++- app/src/main/res/layout/activity_main.xml | 176 +++++++++++++++--- app/src/main/res/layout/the_widget.xml | 78 +++++--- app/src/main/res/values-it-rIT/strings.xml | 10 +- app/src/main/res/values/strings.xml | 10 +- 14 files changed, 545 insertions(+), 135 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3bcc82e..e5378b9 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,8 +15,8 @@ android { applicationId "com.tommasoberlose.anotherwidget" minSdkVersion 19 targetSdkVersion 26 - versionCode 15 - versionName "1.1" + versionCode 16 + versionName "1.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { @@ -50,4 +50,5 @@ dependencies { compile 'com.android.support:recyclerview-v7:26.1.0' compile 'com.google.android.gms:play-services-awareness:11.4.2' compile 'joda-time:joda-time:2.9.9' + compile 'com.pes.materialcolorpicker:library:1.0.4' } diff --git a/app/release/output.json b/app/release/output.json index 5616265..4fc4267 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":15},"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":16},"path":"app-release.apk","properties":{"packageId":"com.tommasoberlose.anotherwidget","split":"","minSdkVersion":"19"}}] \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/object/CalendarSelector.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/object/CalendarSelector.kt index 8600bc9..a58025f 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/object/CalendarSelector.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/object/CalendarSelector.kt @@ -3,14 +3,14 @@ package com.tommasoberlose.anotherwidget.`object` /** * Created by tommaso on 08/10/17. */ -class CalendarSelector(id: Int, name: String, account_name: String) { +class CalendarSelector(id: Int, name: String?, account_name: String?) { var id: Int = 0 var name: String = "" var account_name: String = "" init { this.id = id - this.name = name - this.account_name = account_name + this.name = name?: "" + this.account_name = account_name?: "" } } \ 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 d2957ec..9ffada9 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/object/Constants.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Constants.kt @@ -35,6 +35,7 @@ object Constants { val PREF_NEXT_EVENT_NAME = "PREF_NEXT_EVENT_NAME" val PREF_NEXT_EVENT_START_DATE = "PREF_NEXT_EVENT_START_DATE" val PREF_NEXT_EVENT_ALL_DAY = "PREF_NEXT_EVENT_ALL_DAY" + val PREF_NEXT_EVENT_LOCATION = "PREF_NEXT_EVENT_LOCATION" val PREF_NEXT_EVENT_END_DATE = "PREF_NEXT_EVENT_END_DATE" val PREF_NEXT_EVENT_CALENDAR_ID = "PREF_NEXT_EVENT_CALENDAR_ID" val PREF_CUSTOM_LOCATION_LAT = "PREF_CUSTOM_LOCATION_LAT" @@ -51,6 +52,10 @@ object Constants { 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 PREF_SHOW_EVENT_LOCATION = "PREF_SHOW_EVENT_LOCATION" + val PREF_TEXT_COLOR = "PREF_TEXT_COLOR" + val PREF_TEXT_MAIN_SIZE = "PREF_TEXT_MAIN_SIZE" + val PREF_TEXT_SECOND_SIZE = "PREF_TEXT_SECOND_SIZE" val ACTION_EXTRA_OPEN_WEATHER_PROVIDER = "ACTION_EXTRA_OPEN_WEATHER_PROVIDER" 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 d4a869f..a7348ed 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/object/Event.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Event.kt @@ -15,14 +15,16 @@ class Event { var endDate: Long = 0 var calendarID: Int = 0 var allDay: Boolean = false + var address: String = "" - constructor(id:Int, title:String, startDate:Long, endDate:Long, calendarID: Int, allDay: Boolean) { + constructor(id:Int, title:String, startDate:Long, endDate:Long, calendarID: Int, allDay: Boolean, address: String) { this.id = id this.title = title this.startDate = startDate this.endDate = endDate this.calendarID = calendarID this.allDay = allDay + this.address = address } constructor(eventCursor: Cursor, instanceCursor: Cursor) { @@ -33,6 +35,7 @@ class Event { title = eventCursor.getString(0) allDay = !eventCursor.getString(1).equals("0") calendarID = eventCursor.getInt(2) + address = eventCursor.getString(3) } override fun toString(): String { 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 ddb9828..c01a719 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 @@ -23,13 +23,18 @@ import android.content.BroadcastReceiver import com.tommasoberlose.anotherwidget.util.CalendarUtil import com.tommasoberlose.anotherwidget.util.WeatherUtil import android.content.DialogInterface +import android.graphics.Color import android.graphics.drawable.Drawable import android.os.Build +import android.support.annotation.ColorInt import android.support.design.widget.BottomSheetDialog +import android.support.v4.content.ContextCompat import android.text.Html import android.util.Log +import android.util.TypedValue import android.widget.Toast import com.crashlytics.android.Crashlytics +import com.pes.androidmaterialcolorpickerdialog.ColorPicker import com.tommasoberlose.anotherwidget.`object`.CalendarSelector import io.fabric.sdk.android.Fabric import kotlinx.android.synthetic.main.activity_main.* @@ -244,37 +249,64 @@ class MainActivity : AppCompatActivity() { val e = CalendarUtil.getNextEvent(this) if (e.id != 0) { - next_event.text = Util.getDifferenceText(this, e.title, now.timeInMillis, e.startDate) + next_event.text = e.title + next_event_difference_time.text = Util.getDifferenceText(this, 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) - val endHour: String = if (SP.getString(Constants.PREF_HOUR_FORMAT, "12").equals("12")) Constants.badHourFormat.format(e.endDate) else Constants.goodHourFormat.format(e.endDate) - var dayDiff = TimeUnit.MILLISECONDS.toDays(e.endDate - e.startDate) - - val startCal = Calendar.getInstance() - startCal.timeInMillis = e.startDate - - val endCal = Calendar.getInstance() - endCal.timeInMillis = e.endDate - - 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 = "" - if (dayDiff > 0) { - multipleDay = String.format(" (+%s%s)", dayDiff, getString(R.string.day_char)) - } - next_event_date.text = String.format("%s - %s%s", startHour, endHour, multipleDay) + if (!e.address.equals("") && SP.getBoolean(Constants.PREF_SHOW_EVENT_LOCATION, false)) { + second_row_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_action_location)) + next_event_date.text = e.address } else { - next_event_date.text = dateStringValue + second_row_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_action_calendar)) + 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) + val endHour: String = if (SP.getString(Constants.PREF_HOUR_FORMAT, "12").equals("12")) Constants.badHourFormat.format(e.endDate) else Constants.goodHourFormat.format(e.endDate) + var dayDiff = TimeUnit.MILLISECONDS.toDays(e.endDate - e.startDate) + + val startCal = Calendar.getInstance() + startCal.timeInMillis = e.startDate + + val endCal = Calendar.getInstance() + endCal.timeInMillis = e.endDate + + 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 = "" + if (dayDiff > 0) { + multipleDay = String.format(" (+%s%s)", dayDiff, getString(R.string.day_char)) + } + next_event_date.text = String.format("%s - %s%s", startHour, endHour, multipleDay) + } else { + next_event_date.text = dateStringValue + } } empty_layout.visibility = View.GONE calendar_layout.visibility = View.VISIBLE } } + + empty_date.setTextColor(Util.getFontColor(SP)) + divider1.setTextColor(Util.getFontColor(SP)) + temp.setTextColor(Util.getFontColor(SP)) + next_event.setTextColor(Util.getFontColor(SP)) + next_event_difference_time.setTextColor(Util.getFontColor(SP)) + next_event_date.setTextColor(Util.getFontColor(SP)) + divider2.setTextColor(Util.getFontColor(SP)) + calendar_temp.setTextColor(Util.getFontColor(SP)) + second_row_icon.setColorFilter(Util.getFontColor(SP)) + + + empty_date.setTextSize(TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f)) + divider1.setTextSize(TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f)) + temp.setTextSize(TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f)) + next_event.setTextSize(TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f)) + next_event_difference_time.setTextSize(TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f)) + next_event_date.setTextSize(TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f)) + divider2.setTextSize(TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f)) + calendar_temp.setTextSize(TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f)) } fun updateLocationView() { @@ -388,6 +420,59 @@ class MainActivity : AppCompatActivity() { updateAppWidget() } + show_location_label.text = if (SP.getBoolean(Constants.PREF_SHOW_EVENT_LOCATION, false)) getString(R.string.settings_show_location_subtitle_true) else getString(R.string.settings_show_location_subtitle_false) + action_show_location.setOnClickListener { + SP.edit().putBoolean(Constants.PREF_SHOW_EVENT_LOCATION, !SP.getBoolean(Constants.PREF_SHOW_EVENT_LOCATION, false)).commit() + Util.updateWidget(this) + updateSettings() + updateAppWidget() + } + + main_text_size_label.text = String.format("%.0f%s", SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f), "sp") + action_main_text_size.setOnClickListener { + var fontSize = SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f) + 1 + if (fontSize > 30) { + fontSize = 20f + } + SP.edit().putFloat(Constants.PREF_TEXT_MAIN_SIZE, fontSize).commit() + Util.updateWidget(this) + updateSettings() + updateAppWidget() + } + + second_text_size_label.text = String.format("%.0f%s", SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f), "sp") + action_second_text_size.setOnClickListener { + var fontSize = SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f) + 1 + if (fontSize > 20) { + fontSize = 12f + } + SP.edit().putFloat(Constants.PREF_TEXT_SECOND_SIZE, fontSize).commit() + Util.updateWidget(this) + updateSettings() + updateAppWidget() + } + + + val textColor = try { + Color.parseColor(SP.getString(Constants.PREF_TEXT_COLOR, "#FFFFFF")) + } catch (e: Exception) { + SP.edit().remove(Constants.PREF_TEXT_COLOR).commit() + Color.parseColor(SP.getString(Constants.PREF_TEXT_COLOR, "#FFFFFF")) + } + text_color_icon.setCardBackgroundColor(textColor) + font_color_label.text = SP.getString(Constants.PREF_TEXT_COLOR, "#FFFFFF").toUpperCase() + action_font_color.setOnClickListener { + val cp: ColorPicker = ColorPicker(this@MainActivity, Color.red(textColor), Color.green(textColor), Color.blue(textColor)) + cp.setOnColorSelected { color -> + SP.edit().putString(Constants.PREF_TEXT_COLOR, "#" + Integer.toHexString(color)).commit() + Util.updateWidget(this) + updateSettings() + updateAppWidget() + cp.dismiss() + } + cp.show() + } + val now = Calendar.getInstance() var dateStringValue: String = String.format("%s%s", Constants.engDateFormat.format(now.time)[0].toUpperCase(), Constants.engDateFormat.format(now.time).substring(1)) if (SP.getBoolean(Constants.PREF_ITA_FORMAT_DATE, false)) { @@ -436,6 +521,22 @@ class MainActivity : AppCompatActivity() { startActivityForResult(Intent(this, CustomLocationActivity::class.java), Constants.RESULT_CODE_CUSTOM_LOCATION) } + if (SP.getString(Constants.PREF_CUSTOM_LOCATION_ADD, getString(R.string.custom_location_gps)) == getString(R.string.custom_location_gps)) { + action_weather_provider_api_key.visibility= View.GONE + } else { + action_weather_provider_api_key.visibility= View.VISIBLE + 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 + } else { + label_weather_provider_api_key.text = getString(R.string.settings_weather_provider_api_key_subtitle_not_set) + alert_icon.visibility = View.VISIBLE + } + action_weather_provider_api_key.setOnClickListener { + startActivityForResult(Intent(this, WeatherProviderActivity::class.java), Constants.WEATHER_PROVIDER_REQUEST_CODE) + } + } + calendar_app_label.text = SP.getString(Constants.PREF_CALENDAR_APP_NAME, getString(R.string.default_name)) action_calendar_app.setOnClickListener { startActivityForResult(Intent(this, ChooseApplicationActivity::class.java), Constants.CALENDAR_APP_REQUEST_CODE) @@ -451,17 +552,6 @@ class MainActivity : AppCompatActivity() { 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 - } else { - label_weather_provider_api_key.text = getString(R.string.settings_weather_provider_api_key_subtitle_not_set) - alert_icon.visibility = View.VISIBLE - } - action_weather_provider_api_key.setOnClickListener { - startActivityForResult(Intent(this, WeatherProviderActivity::class.java), Constants.WEATHER_PROVIDER_REQUEST_CODE) - } - action_filter_calendar.setOnClickListener { val calendarSelectorList: List = CalendarUtil.getCalendarList(this) var calFiltered = SP.getString(Constants.PREF_CALENDAR_FILTER, "") 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 06019e5..339e301 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 @@ -29,12 +29,12 @@ import android.net.Uri import android.widget.TextClock import android.widget.TextView import android.content.ComponentName +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.drawable.BitmapDrawable import android.support.v4.content.ContextCompat.startActivity import android.provider.CalendarContract.Events - - - - +import android.util.TypedValue /** @@ -70,6 +70,25 @@ class TheWidget : AppWidgetProvider() { views = updateLocationView(context, views, appWidgetId) + val SP = PreferenceManager.getDefaultSharedPreferences(context) + views.setTextColor(R.id.empty_date, Util.getFontColor(SP)) + views.setTextColor(R.id.divider1, Util.getFontColor(PreferenceManager.getDefaultSharedPreferences(context))) + views.setTextColor(R.id.temp, Util.getFontColor(PreferenceManager.getDefaultSharedPreferences(context))) + views.setTextColor(R.id.next_event, Util.getFontColor(PreferenceManager.getDefaultSharedPreferences(context))) + views.setTextColor(R.id.next_event_difference_time, Util.getFontColor(PreferenceManager.getDefaultSharedPreferences(context))) + views.setTextColor(R.id.next_event_date, Util.getFontColor(PreferenceManager.getDefaultSharedPreferences(context))) + views.setTextColor(R.id.divider2, Util.getFontColor(PreferenceManager.getDefaultSharedPreferences(context))) + views.setTextColor(R.id.calendar_temp, Util.getFontColor(PreferenceManager.getDefaultSharedPreferences(context))) + + views.setTextViewTextSize(R.id.empty_date, TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f)) + views.setTextViewTextSize(R.id.divider1, TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f)) + views.setTextViewTextSize(R.id.temp, TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f)) + views.setTextViewTextSize(R.id.next_event, TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f)) + views.setTextViewTextSize(R.id.next_event_difference_time, TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f)) + views.setTextViewTextSize(R.id.next_event_date, TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f)) + views.setTextViewTextSize(R.id.divider2, TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f)) + views.setTextViewTextSize(R.id.calendar_temp, TypedValue.COMPLEX_UNIT_SP, SP.getFloat(Constants.PREF_TEXT_SECOND_SIZE, 16f)) + appWidgetManager.updateAppWidget(appWidgetId, views) } @@ -96,40 +115,55 @@ class TheWidget : AppWidgetProvider() { val e = CalendarUtil.getNextEvent(context) if (e.id != 0) { - views.setTextViewText(R.id.next_event, Util.getDifferenceText(context, e.title, now.timeInMillis, e.startDate)) + views.setTextViewText(R.id.next_event, e.title) + views.setTextViewText(R.id.next_event_difference_time, Util.getDifferenceText(context, 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) - val endHour: String = if (SP.getString(Constants.PREF_HOUR_FORMAT, "12").equals("12")) Constants.badHourFormat.format(e.endDate) else Constants.goodHourFormat.format(e.endDate) - var dayDiff = TimeUnit.MILLISECONDS.toDays(e.endDate - e.startDate) + if (!e.address.equals("") && SP.getBoolean(Constants.PREF_SHOW_EVENT_LOCATION, false)) { - val startCal = Calendar.getInstance() - startCal.timeInMillis = e.startDate + val source = BitmapFactory.decodeResource(context.resources, R.drawable.ic_action_location); + val result = Util.changeBitmapColor(source, Util.getFontColor(SP)) + views.setImageViewBitmap(R.id.second_row_icon, result) - val endCal = Calendar.getInstance() - endCal.timeInMillis = e.endDate + views.setTextViewText(R.id.next_event_date, e.address) - 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 = "" - if (dayDiff > 0) { - multipleDay = String.format(" (+%s%s)", dayDiff, context.getString(R.string.day_char)) - } - - views.setTextViewText(R.id.next_event_date, String.format("%s - %s%s", startHour, endHour, multipleDay)) + val mapIntent = PendingIntent.getActivity(context, widgetID, Util.getGoogleMapsIntentFromAddress(context, e.address), 0) + views.setOnClickPendingIntent(R.id.next_event_date, mapIntent) } else { - views.setTextViewText(R.id.next_event_date, dateStringValue) + val source = BitmapFactory.decodeResource(context.resources, R.drawable.ic_action_calendar); + val result = Util.changeBitmapColor(source, Util.getFontColor(SP)) + views.setImageViewBitmap(R.id.second_row_icon, result) + + 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) + val endHour: String = if (SP.getString(Constants.PREF_HOUR_FORMAT, "12").equals("12")) Constants.badHourFormat.format(e.endDate) else Constants.goodHourFormat.format(e.endDate) + var dayDiff = TimeUnit.MILLISECONDS.toDays(e.endDate - e.startDate) + + val startCal = Calendar.getInstance() + startCal.timeInMillis = e.startDate + + val endCal = Calendar.getInstance() + endCal.timeInMillis = e.endDate + + 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 = "" + if (dayDiff > 0) { + multipleDay = String.format(" (+%s%s)", dayDiff, context.getString(R.string.day_char)) + } + + views.setTextViewText(R.id.next_event_date, String.format("%s - %s%s", startHour, endHour, multipleDay)) + } else { + views.setTextViewText(R.id.next_event_date, dateStringValue) + } } views.setViewVisibility(R.id.empty_layout, View.GONE) views.setViewVisibility(R.id.calendar_layout, View.VISIBLE) - - val pIntent = PendingIntent.getActivity(context, widgetID, Util.getEventIntent(context, e), 0) views.setOnClickPendingIntent(R.id.main_layout, pIntent) } 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 d402cd4..e5a8a4f 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt @@ -55,7 +55,7 @@ object CalendarUtil { for (i in 0 until instanceCursor.count) { val ID = instanceCursor.getInt(0) - val eventCursor = context.contentResolver.query(CalendarContract.Events.CONTENT_URI, arrayOf(CalendarContract.Events.TITLE, CalendarContract.Events.ALL_DAY, CalendarContract.Events.CALENDAR_ID), + val eventCursor = context.contentResolver.query(CalendarContract.Events.CONTENT_URI, arrayOf(CalendarContract.Events.TITLE, CalendarContract.Events.ALL_DAY, CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.EVENT_LOCATION), CalendarContract.Events._ID + " is ?", arrayOf(Integer.toString(ID)), null) @@ -109,7 +109,7 @@ object CalendarUtil { try { val calendarCursor = context.contentResolver.query(Uri.parse("content://com.android.calendar/calendars"), - arrayOf(CalendarContract.Calendars._ID, CalendarContract.Calendars.NAME, CalendarContract.Calendars.ACCOUNT_NAME), + arrayOf(CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.ACCOUNT_NAME), null, null, null) @@ -119,7 +119,10 @@ object CalendarUtil { calendarCursor.moveToFirst() for (j in 0 until calendarCursor.count) { - calendarList.add(CalendarSelector(calendarCursor.getInt(0), calendarCursor.getString(1), calendarCursor.getString(2))) + val id = calendarCursor.getInt(0) + val name = calendarCursor.getString(1) + val account = calendarCursor.getString(2) + calendarList.add(CalendarSelector(id, name, account)) calendarCursor.moveToNext() } } else { @@ -131,7 +134,7 @@ object CalendarUtil { ignored.printStackTrace() try { val calendarCursor = context.contentResolver.query(CalendarContract.Calendars.CONTENT_URI, - arrayOf(CalendarContract.Calendars._ID, CalendarContract.Calendars.NAME, CalendarContract.Calendars.ACCOUNT_NAME), + arrayOf(CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.ACCOUNT_NAME), null, null, null) @@ -167,6 +170,7 @@ object CalendarUtil { .remove(Constants.PREF_NEXT_EVENT_END_DATE) .remove(Constants.PREF_NEXT_EVENT_ALL_DAY) .remove(Constants.PREF_NEXT_EVENT_CALENDAR_ID) + .remove(Constants.PREF_NEXT_EVENT_LOCATION) .commit() Util.updateWidget(context) } @@ -181,12 +185,13 @@ object CalendarUtil { .putLong(Constants.PREF_NEXT_EVENT_END_DATE, event.endDate) .putBoolean(Constants.PREF_NEXT_EVENT_ALL_DAY, event.allDay) .putInt(Constants.PREF_NEXT_EVENT_CALENDAR_ID, event.calendarID) + .putString(Constants.PREF_NEXT_EVENT_LOCATION, event.address) .commit() Util.updateWidget(context) } fun getNextEvent(context: Context): Event { val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - return Event(SP.getInt(Constants.PREF_NEXT_EVENT_ID, 0), SP.getString(Constants.PREF_NEXT_EVENT_NAME, ""), SP.getLong(Constants.PREF_NEXT_EVENT_START_DATE, 0), SP.getLong(Constants.PREF_NEXT_EVENT_END_DATE, 0), SP.getInt(Constants.PREF_NEXT_EVENT_CALENDAR_ID, 0), SP.getBoolean(Constants.PREF_NEXT_EVENT_ALL_DAY, false)) + return Event(SP.getInt(Constants.PREF_NEXT_EVENT_ID, 0), SP.getString(Constants.PREF_NEXT_EVENT_NAME, ""), SP.getLong(Constants.PREF_NEXT_EVENT_START_DATE, 0), SP.getLong(Constants.PREF_NEXT_EVENT_END_DATE, 0), SP.getInt(Constants.PREF_NEXT_EVENT_CALENDAR_ID, 0), SP.getBoolean(Constants.PREF_NEXT_EVENT_ALL_DAY, false), SP.getString(Constants.PREF_NEXT_EVENT_LOCATION, "")) } } \ 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 c20ab5b..0d2c381 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt @@ -1,6 +1,7 @@ package com.tommasoberlose.anotherwidget.util import android.Manifest +import android.annotation.SuppressLint import android.app.Notification import android.app.NotificationManager import android.app.PendingIntent @@ -29,6 +30,7 @@ import android.content.ComponentName import android.preference.PreferenceManager import android.provider.CalendarContract import android.provider.Settings +import android.support.v4.graphics.drawable.DrawableCompat import android.util.Log import android.view.View import android.view.animation.Animation @@ -116,6 +118,20 @@ object Util { context.startActivity(Intent.createChooser(sendIntent, context.getString(R.string.action_share))); } + fun getGoogleMapsIntentFromAddress(context: Context, address:String): Intent { + val gmmIntentUri: Uri = Uri.parse("geo:0,0?q=" + address); + val mapIntent = Intent(Intent.ACTION_VIEW, gmmIntentUri); + mapIntent.`package` = "com.google.android.apps.maps"; + + return if (mapIntent.resolveActivity(context.packageManager) != null) { + mapIntent + } else { + val map = "http://maps.google.co.in/maps?q=" + address + val i = Intent(Intent.ACTION_VIEW, Uri.parse(map)); + i + } + } + fun getCurrentWallpaper(context: Context): Drawable? { var wallpaper: Drawable? = null try { @@ -252,7 +268,7 @@ object Util { } else { val pm: PackageManager = context.packageManager return try { - val intent: Intent = pm.getLaunchIntentForPackage(SP.getString(Constants.PREF_CALENDAR_APP_PACKAGE, "")) + val intent: Intent = pm.getLaunchIntentForPackage(SP.getString(Constants.PREF_EVENT_APP_PACKAGE, "")) intent.addCategory(Intent.CATEGORY_LAUNCHER) intent } catch (ex: Exception) { @@ -391,14 +407,14 @@ object Util { return String(Character.toChars(unicode)) } - fun getDifferenceText(context: Context, title: String, now: Long, start: Long): String { + fun getDifferenceText(context: Context, 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) + return "" } else if (difference < 1000 * 60) { val minutes = TimeUnit.MILLISECONDS.toMinutes(difference) var time = "" @@ -406,7 +422,7 @@ object Util { time += "" + minutes + context.getString(R.string.min_code) } - return String.format("%s %s %s", title, context.getString(R.string.in_code), time) + return String.format("%s %s", context.getString(R.string.in_code), time) } else if (difference < 1000 * 60 * 6) { val hour = TimeUnit.MILLISECONDS.toHours(difference) var time = "" @@ -422,16 +438,45 @@ object Util { time += "" + minutes + context.getString(R.string.min_code) } - return String.format("%s %s %s", title, context.getString(R.string.in_code), time) + return String.format("%s %s", 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)) + return String.format("%s", context.getString(R.string.tomorrow)) } else if (eventDate.dayOfYear == nowDate.dayOfYear) { - return String.format("%s %s", title, context.getString(R.string.today)) + return String.format("%s", 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)) + return String.format("%s %s%s", context.getString(R.string.in_code), days, context.getString(R.string.day_char)) } } + @SuppressLint("ApplySharedPref") + fun getFontColor(SP: SharedPreferences): Int { + return try { + Color.parseColor(SP.getString(Constants.PREF_TEXT_COLOR, "#FFFFFF")) + } catch (e: Exception) { + SP.edit().remove(Constants.PREF_TEXT_COLOR).commit() + Color.parseColor(SP.getString(Constants.PREF_TEXT_COLOR, "#FFFFFF")) + } + } + + fun getTintedDrawable(context: Context, inputDrawable: Int, color: Int): Drawable { + val wrapDrawable = ContextCompat.getDrawable(context, inputDrawable); + DrawableCompat.setTint(wrapDrawable, color); + DrawableCompat.setTintMode(wrapDrawable, PorterDuff.Mode.SRC_IN); + return wrapDrawable; + } + + fun changeBitmapColor(sourceBitmap: Bitmap, color: Int): Bitmap { + val resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, + sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1); + val p = Paint() + val filter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN); + p.colorFilter = filter; + + val canvas = Canvas(resultBitmap); + canvas.drawBitmap(resultBitmap, 0f, 0f, p); + + return resultBitmap; + } } 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 3505b6e..2c5cb91 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt @@ -26,6 +26,7 @@ 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 +import java.util.* /** @@ -89,7 +90,7 @@ object WeatherUtil { 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_ICON, getIconCodeFromAwareness(weather.conditions)) .putString(Constants.PREF_WEATHER_REAL_TEMP_UNIT, SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F")) .commit() Util.updateWidget(context) @@ -159,6 +160,39 @@ object WeatherUtil { Util.updateWidget(context) } + fun getIconCodeFromAwareness(conditions: IntArray): String { + var icon = "" + return if (conditions.contains(Weather.CONDITION_UNKNOWN)) { + "" + } else { + if (conditions.contains(Weather.CONDITION_CLEAR)) { + icon = "01" + } else if (conditions.contains(Weather.CONDITION_CLOUDY)) { + icon = "02" + } else if (conditions.contains(Weather.CONDITION_RAINY)) { + icon = "10" + } else if (conditions.contains(Weather.CONDITION_STORMY)) { + icon = "09" + } else if (conditions.contains(Weather.CONDITION_SNOWY)) { + icon = "13" + } else if (conditions.contains(Weather.CONDITION_WINDY)) { + icon = "80" + } else if (conditions.contains(Weather.CONDITION_HAZY)) { + icon = "50" + } else if (conditions.contains(Weather.CONDITION_ICY)) { + icon = "81" + } else if (conditions.contains(Weather.CONDITION_FOGGY)) { + icon = "82" + } + + return if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 19 || Calendar.getInstance().get(Calendar.HOUR_OF_DAY) < 7) { + icon + "n" + } else { + icon + "d" + } + } + } + fun getWeatherIconResource(icon: String): Int { when (icon) { "01d" -> { @@ -188,6 +222,18 @@ object WeatherUtil { "50d" -> { return R.drawable.haze_day } + "80d" -> { + return R.drawable.windy_day + } + "81d" -> { + return R.drawable.rain_snow_day + } + "82d" -> { + return R.drawable.haze_weather + } + + + "01n" -> { return R.drawable.clear_night } @@ -215,8 +261,17 @@ object WeatherUtil { "50n" -> { return R.drawable.haze_night } + "80n" -> { + return R.drawable.windy_night + } + "81n" -> { + return R.drawable.rain_snow_night + } + "82n" -> { + return R.drawable.haze_weather + } else -> { - return -1 + return R.drawable.unknown } } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 5cfa385..9498bf7 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,7 +1,7 @@ - @@ -69,6 +69,106 @@ android:orientation="vertical" android:paddingTop="16dp" android:paddingBottom="16dp"> + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - @@ -55,31 +60,58 @@ android:visibility="gone" android:id="@+id/calendar_layout" android:gravity="center"> - + android:orientation="horizontal"> + + + - - + android:gravity="center_vertical" + android:orientation="horizontal"> + + + @@ -89,11 +121,12 @@ android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="2dp" + android:id="@+id/divider2" android:text="@string/divider" style="@style/AnotherWidget.Subtitle"/> diff --git a/app/src/main/res/values-it-rIT/strings.xml b/app/src/main/res/values-it-rIT/strings.xml index 4ed894e..1af3afb 100644 --- a/app/src/main/res/values-it-rIT/strings.xml +++ b/app/src/main/res/values-it-rIT/strings.xml @@ -95,5 +95,13 @@ Ben Fatto! dieci minuti prima che la tua chiave sia attivata.

Quindi rilassati! Il meteo verrĂ  aggiornato non appena sarĂ  disponibile!]]>
" ore" - domani + Domani + Oggi + Gli eventi aprono + Informazione Seconda Riga + Mostra orario evento + Mostra indirizzo evento invece dell\'orario + Colore Testo + Dimensione Testo Prima Riga + Dimensione Testo Seconda Riga \ 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 8128bca..4ab8a62 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -97,7 +97,13 @@ ten minutes before your API key is activated.

So relax! The weather will be updated as soon as it is available!!]]>
Well Done! h - tomorrow - oggi + Tomorrow + Today Tap on event opens + Second Row Information + Show event address instead of time + Show event time + Text Color + First Row Text Size + Second Row Text Size