From f62f9dc3bd2d03ad83cc140f79de941a2ca20f10 Mon Sep 17 00:00:00 2001 From: Tommaso Berlose Date: Mon, 9 Oct 2017 09:41:47 +0200 Subject: [PATCH] Update UI, Update project structure, bug fixes --- app/build.gradle | 2 +- app/release/output.json | 2 +- app/src/main/AndroidManifest.xml | 6 +- .../anotherwidget/object/Calendar.kt | 16 ++ .../anotherwidget/object/Constants.kt | 2 + .../NewCalendarEventReceiver.kt | 3 +- .../{util => receiver}/UpdatesReceiver.kt | 5 +- .../{util => receiver}/WeatherReceiver.kt | 8 +- .../anotherwidget/ui/activity/MainActivity.kt | 42 ++-- .../anotherwidget/ui/widget/TheWidget.kt | 36 ++-- .../anotherwidget/util/CalendarUtil.kt | 87 ++++++++ .../tommasoberlose/anotherwidget/util/Util.kt | 193 ------------------ .../anotherwidget/util/WeatherUtil.kt | 159 +++++++++++++++ app/src/main/res/layout/activity_main.xml | 49 +++++ app/src/main/res/layout/the_widget.xml | 3 +- app/src/main/res/values-it-rIT/strings.xml | 5 + app/src/main/res/values/strings.xml | 5 + app/src/main/res/xml/the_widget_info.xml | 8 +- 18 files changed, 383 insertions(+), 248 deletions(-) create mode 100644 app/src/main/java/com/tommasoberlose/anotherwidget/object/Calendar.kt rename app/src/main/java/com/tommasoberlose/anotherwidget/{util => receiver}/NewCalendarEventReceiver.kt (72%) rename app/src/main/java/com/tommasoberlose/anotherwidget/{util => receiver}/UpdatesReceiver.kt (93%) rename app/src/main/java/com/tommasoberlose/anotherwidget/{util => receiver}/WeatherReceiver.kt (85%) create mode 100644 app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt create mode 100644 app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt diff --git a/app/build.gradle b/app/build.gradle index aaf39a6..9f732b1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,7 +13,7 @@ android { applicationId "com.tommasoberlose.anotherwidget" minSdkVersion 19 targetSdkVersion 26 - versionCode 3 + versionCode 5 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } diff --git a/app/release/output.json b/app/release/output.json index 9429200..76b5fcc 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":3},"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":5},"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 370b8e5..b033bde 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -36,7 +36,7 @@ android:resource="@xml/the_widget_info" /> @@ -47,7 +47,7 @@ @@ -55,7 +55,7 @@ diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/object/Calendar.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Calendar.kt new file mode 100644 index 0000000..420cc71 --- /dev/null +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Calendar.kt @@ -0,0 +1,16 @@ +package com.tommasoberlose.anotherwidget.`object` + +/** + * Created by tommaso on 08/10/17. + */ +class Calendar(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 + } +} \ 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 b4efcc9..b42fb80 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/object/Constants.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/object/Constants.kt @@ -15,6 +15,8 @@ object Constants { val PREF_WEATHER_ICON = "PREF_WEATHER_ICON" val PREF_WEATHER_TEMP = "PREF_WEATHER_TEMP" val PREF_WEATHER_TEMP_UNIT = "PREF_WEATHER_TEMP_UNIT" + val PREF_CALENDAR_ALL_DAY = "PREF_CALENDAR_ALL_DAY" + val PREF_CALENDAR_FILTER = "PREF_CALENDAR_FILTER" val dateFormat = SimpleDateFormat("EEEE, MMM d", Locale.getDefault()) val hourFormat = SimpleDateFormat("HH:mm", Locale.getDefault()) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/NewCalendarEventReceiver.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/NewCalendarEventReceiver.kt similarity index 72% rename from app/src/main/java/com/tommasoberlose/anotherwidget/util/NewCalendarEventReceiver.kt rename to app/src/main/java/com/tommasoberlose/anotherwidget/receiver/NewCalendarEventReceiver.kt index e6fe475..ef39df2 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/NewCalendarEventReceiver.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/NewCalendarEventReceiver.kt @@ -1,8 +1,9 @@ -package com.tommasoberlose.anotherwidget.util +package com.tommasoberlose.anotherwidget.receiver import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import com.tommasoberlose.anotherwidget.util.Util class NewCalendarEventReceiver : BroadcastReceiver() { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/UpdatesReceiver.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/UpdatesReceiver.kt similarity index 93% rename from app/src/main/java/com/tommasoberlose/anotherwidget/util/UpdatesReceiver.kt rename to app/src/main/java/com/tommasoberlose/anotherwidget/receiver/UpdatesReceiver.kt index b450fe0..03b8d2c 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/UpdatesReceiver.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/UpdatesReceiver.kt @@ -1,14 +1,13 @@ -package com.tommasoberlose.anotherwidget.util +package com.tommasoberlose.anotherwidget.receiver import android.Manifest import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import android.R.string.cancel import android.app.AlarmManager import android.app.PendingIntent -import android.util.Log import com.tommasoberlose.anotherwidget.`object`.Constants +import com.tommasoberlose.anotherwidget.util.Util class UpdatesReceiver : BroadcastReceiver() { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherReceiver.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/WeatherReceiver.kt similarity index 85% rename from app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherReceiver.kt rename to app/src/main/java/com/tommasoberlose/anotherwidget/receiver/WeatherReceiver.kt index d6a32e8..c829a9c 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherReceiver.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receiver/WeatherReceiver.kt @@ -1,4 +1,4 @@ -package com.tommasoberlose.anotherwidget.util +package com.tommasoberlose.anotherwidget.receiver import android.Manifest import android.app.AlarmManager @@ -7,12 +7,14 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import com.tommasoberlose.anotherwidget.`object`.Constants +import com.tommasoberlose.anotherwidget.util.Util +import com.tommasoberlose.anotherwidget.util.WeatherUtil class WeatherReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (intent.action.equals(Intent.ACTION_BOOT_COMPLETED) || intent.action.equals(Intent.ACTION_INSTALL_PACKAGE) || intent.action.equals(Constants.ACTION_WEATHER_UPDATE)) { - Util.getWeather(context) + WeatherUtil.getWeather(context) } } @@ -20,7 +22,7 @@ class WeatherReceiver : BroadcastReceiver() { removeUpdates(context) if (Util.checkGrantedPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)) { - Util.getWeather(context) + WeatherUtil.getWeather(context) val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager val i = Intent(context, WeatherReceiver::class.java) 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 f0779b6..f392ea5 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 @@ -2,7 +2,6 @@ package com.tommasoberlose.anotherwidget.ui.activity import android.Manifest import android.annotation.SuppressLint -import android.app.PendingIntent import android.content.pm.PackageManager import android.support.v7.app.AppCompatActivity import android.os.Bundle @@ -10,25 +9,20 @@ import android.support.v4.app.ActivityCompat import android.appwidget.AppWidgetManager import android.content.* import android.preference.PreferenceManager -import android.provider.CalendarContract -import android.support.v4.content.ContextCompat import android.view.View -import android.widget.RemoteViews -import android.widget.Toast import com.tommasoberlose.anotherwidget.`object`.Constants import com.tommasoberlose.anotherwidget.R import com.tommasoberlose.anotherwidget.util.Util -import com.tommasoberlose.anotherwidget.ui.widget.TheWidget -import com.tommasoberlose.anotherwidget.util.UpdatesReceiver -import com.tommasoberlose.anotherwidget.util.WeatherReceiver +import com.tommasoberlose.anotherwidget.receiver.UpdatesReceiver +import com.tommasoberlose.anotherwidget.receiver.WeatherReceiver import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.the_widget.* import java.util.* import java.util.concurrent.TimeUnit import android.content.Intent import android.content.BroadcastReceiver - - +import com.tommasoberlose.anotherwidget.util.CalendarUtil +import com.tommasoberlose.anotherwidget.util.WeatherUtil class MainActivity : AppCompatActivity() { @@ -121,7 +115,6 @@ class MainActivity : AppCompatActivity() { } } updateAppWidget() - Util.updateWidget(this) } @@ -140,7 +133,7 @@ class MainActivity : AppCompatActivity() { empty_date.text = String.format("%s%s", Constants.dateFormat.format(now.time)[0].toUpperCase(), Constants.dateFormat.format(now.time).substring(1)) if (calendarLayout) { - val eventList = Util.getNextEvent(this) + val eventList = CalendarUtil.getNextEvent(this) if (eventList.isNotEmpty()) { val difference = eventList[0].startDate - now.timeInMillis @@ -185,8 +178,8 @@ class MainActivity : AppCompatActivity() { weather_icon.visibility = View.GONE empty_weather_icon.visibility = View.GONE } else { - weather_icon.setImageResource(Util.getWeatherIconResource(icon)) - empty_weather_icon.setImageResource(Util.getWeatherIconResource(icon)) + weather_icon.setImageResource(WeatherUtil.getWeatherIconResource(icon)) + empty_weather_icon.setImageResource(WeatherUtil.getWeatherIconResource(icon)) } temp.text = currentTemp @@ -197,17 +190,22 @@ class MainActivity : AppCompatActivity() { } } + @SuppressLint("ApplySharedPref") fun updateSettings() { val SP = PreferenceManager.getDefaultSharedPreferences(this) temp_unit.text = if (SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F").equals("F")) getString(R.string.fahrenheit) else getString(R.string.celsius) - action_change_unit.setOnClickListener(object: View.OnClickListener { - @SuppressLint("ApplySharedPref") - override fun onClick(p0: View?) { - SP.edit().putString(Constants.PREF_WEATHER_TEMP_UNIT, if (SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F").equals("F")) "C" else "F").commit() - Util.getWeather(this@MainActivity) - updateSettings() - } - }) + action_change_unit.setOnClickListener { + SP.edit().putString(Constants.PREF_WEATHER_TEMP_UNIT, if (SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F").equals("F")) "C" else "F").commit() + WeatherUtil.getWeather(this) + updateSettings() + } + + all_day_label.text = if (SP.getBoolean(Constants.PREF_CALENDAR_ALL_DAY, false)) getString(R.string.settings_all_day_subtitle_visible) else getString(R.string.settings_all_day_subtitle_gone) + action_show_all_day.setOnClickListener { + SP.edit().putBoolean(Constants.PREF_CALENDAR_ALL_DAY, !SP.getBoolean(Constants.PREF_CALENDAR_ALL_DAY, false)).commit() + updateUI() + Util.updateWidget(this) + } } } 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 2f99ab8..8e2b1ff 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 @@ -5,30 +5,25 @@ import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider import android.content.Context import android.content.Intent -import android.content.SharedPreferences import android.preference.PreferenceManager -import android.util.Log import android.view.View import android.widget.RemoteViews import com.tommasoberlose.anotherwidget.`object`.Constants -import com.tommasoberlose.anotherwidget.`object`.Event import com.tommasoberlose.anotherwidget.R -import com.tommasoberlose.anotherwidget.ui.activity.MainActivity -import com.tommasoberlose.anotherwidget.util.UpdatesReceiver +import com.tommasoberlose.anotherwidget.receiver.UpdatesReceiver import com.tommasoberlose.anotherwidget.util.Util -import com.tommasoberlose.anotherwidget.util.WeatherReceiver +import com.tommasoberlose.anotherwidget.receiver.WeatherReceiver -import java.text.SimpleDateFormat import java.util.Calendar import java.util.Locale import java.util.concurrent.TimeUnit import android.app.PendingIntent -import android.net.Uri import android.provider.CalendarContract import android.content.ContentUris - - +import android.util.Log +import com.tommasoberlose.anotherwidget.util.CalendarUtil +import com.tommasoberlose.anotherwidget.util.WeatherUtil /** @@ -62,7 +57,7 @@ class TheWidget : AppWidgetProvider() { views = updateCalendarView(context, views, appWidgetId) - views = updateLocationView(context, views) + views = updateLocationView(context, views, appWidgetId) appWidgetManager.updateAppWidget(appWidgetId, views) } @@ -73,7 +68,7 @@ class TheWidget : AppWidgetProvider() { views.setViewVisibility(R.id.empty_layout, View.VISIBLE) views.setViewVisibility(R.id.calendar_layout, View.GONE) - views.setTextViewText(R.id.empty_date, Constants.dateFormat.format(now.time)) + views.setTextViewText(R.id.empty_date, Constants.dateFormat.format(now.time)[0].toUpperCase() + Constants.dateFormat.format(now.time).substring(1)) val calIntent = Intent(Intent.ACTION_MAIN) calIntent.addCategory(Intent.CATEGORY_APP_CALENDAR) @@ -82,7 +77,7 @@ class TheWidget : AppWidgetProvider() { if (calendarLayout) { - val eventList = Util.getNextEvent(context) + val eventList = CalendarUtil.getNextEvent(context) if (eventList.isNotEmpty()) { val difference = eventList[0].startDate - now.timeInMillis @@ -120,7 +115,7 @@ class TheWidget : AppWidgetProvider() { return views } - fun updateLocationView(context: Context, views: RemoteViews): RemoteViews { + fun updateLocationView(context: Context, views: RemoteViews, widgetID: Int): RemoteViews { val locationLayout = Util.checkGrantedPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) val SP = PreferenceManager.getDefaultSharedPreferences(context) @@ -137,12 +132,21 @@ class TheWidget : AppWidgetProvider() { views.setViewVisibility(R.id.weather_icon, View.GONE) views.setViewVisibility(R.id.empty_weather_icon, View.GONE) } else { - views.setImageViewResource(R.id.weather_icon, Util.getWeatherIconResource(icon)) - views.setImageViewResource(R.id.empty_weather_icon, Util.getWeatherIconResource(icon)) + views.setImageViewResource(R.id.weather_icon, WeatherUtil.getWeatherIconResource(icon)) + views.setImageViewResource(R.id.empty_weather_icon, WeatherUtil.getWeatherIconResource(icon)) } views.setTextViewText(R.id.temp, temp) views.setTextViewText(R.id.calendar_temp, temp) + + val weatherIntent = Intent("com.google.android.googlequicksearchbox.GOOGLE_SEARCH") + weatherIntent.addCategory(Intent.CATEGORY_DEFAULT) + weatherIntent.putExtra("type", "string") + weatherIntent.putExtra("query", "weather") + val weatherPIntent = PendingIntent.getActivity(context, widgetID, weatherIntent, 0) + + views.setOnClickPendingIntent(R.id.weather, weatherPIntent) + views.setOnClickPendingIntent(R.id.calendar_weather, weatherPIntent) } else { views.setViewVisibility(R.id.weather, View.GONE) views.setViewVisibility(R.id.calendar_weather, View.GONE) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt new file mode 100644 index 0000000..e7d93b3 --- /dev/null +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/CalendarUtil.kt @@ -0,0 +1,87 @@ +package com.tommasoberlose.anotherwidget.util + +import android.Manifest +import android.content.ContentUris +import android.content.Context +import android.content.SharedPreferences +import android.preference.PreferenceManager +import android.provider.CalendarContract +import com.tommasoberlose.anotherwidget.`object`.Constants +import com.tommasoberlose.anotherwidget.`object`.Event +import java.util.* + +/** + * Created by tommaso on 08/10/17. + */ + +object CalendarUtil { + + fun getNextEvent(context: Context): List { + val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + val eventList = ArrayList() + + val now = Calendar.getInstance() + val hourLimit = Calendar.getInstance() + hourLimit.add(Calendar.HOUR, 6) + + val builder = CalendarContract.Instances.CONTENT_URI.buildUpon() + ContentUris.appendId(builder, now.timeInMillis) + ContentUris.appendId(builder, hourLimit.timeInMillis) + + if (!Util.checkGrantedPermission(context, Manifest.permission.READ_CALENDAR)) { + return eventList + } + + val instanceCursor = context.contentResolver.query(builder.build(), arrayOf(CalendarContract.Instances.EVENT_ID, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END), null, null, null) ?: return eventList + instanceCursor.moveToFirst() + + 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._ID + " is ?", + arrayOf(Integer.toString(ID)), null) ?: return eventList + eventCursor.moveToFirst() + + for (j in 0 until eventCursor.count) { + val e = Event(eventCursor, instanceCursor) + val allDay: Boolean = !eventCursor.getString(1).equals("0") + if (e.endDate - now.timeInMillis > 1000 * 60 * 30 && (SP.getBoolean(Constants.PREF_CALENDAR_ALL_DAY, false) || !allDay)) { + eventList.add(e) + } + eventCursor.moveToNext() + } + + eventCursor.close() + + instanceCursor.moveToNext() + } + + instanceCursor.close() + return eventList + } + fun getCalendarList(context: Context): List { + val calendarList = ArrayList() + + if (!Util.checkGrantedPermission(context, Manifest.permission.READ_CALENDAR)) { + return calendarList + } + + val calendarCursor = context.contentResolver.query(CalendarContract.Calendars.CONTENT_URI, + arrayOf(CalendarContract.Calendars._ID, CalendarContract.Calendars.NAME, CalendarContract.Calendars.ACCOUNT_NAME), + null, + null, + null) ?: return calendarList + + calendarCursor.moveToFirst() + + for (j in 0 until calendarCursor.count) { + calendarList.add(com.tommasoberlose.anotherwidget.`object`.Calendar(calendarCursor.getInt(0), calendarCursor.getString(1), calendarCursor.getString(2))) + calendarCursor.moveToNext() + } + + calendarCursor.close() + + return calendarList + } +} \ 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 c8050a9..860b677 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt @@ -50,49 +50,6 @@ object Util { return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED } - fun getNextEvent(context: Context): List { - val eventList = ArrayList() - - val now = Calendar.getInstance() - val hourLimit = Calendar.getInstance() - hourLimit.add(Calendar.HOUR, 6) - - val builder = CalendarContract.Instances.CONTENT_URI.buildUpon() - ContentUris.appendId(builder, now.timeInMillis) - ContentUris.appendId(builder, hourLimit.timeInMillis) - - if (!checkGrantedPermission(context, Manifest.permission.READ_CALENDAR)) { - return eventList - } - - val instanceCursor = context.contentResolver.query(builder.build(), arrayOf(CalendarContract.Instances.EVENT_ID, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END), null, null, null) ?: return eventList - instanceCursor.moveToFirst() - - 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._ID + " is ?", - arrayOf(Integer.toString(ID)), null) ?: return eventList - eventCursor.moveToFirst() - - for (j in 0 until eventCursor.count) { - val e = Event(eventCursor, instanceCursor) - if (e.endDate - now.timeInMillis > 1000 * 60 * 30) { - eventList.add(e) - } - eventCursor.moveToNext() - } - - eventCursor.close() - - instanceCursor.moveToNext() - } - - instanceCursor.close() - return eventList - } - fun updateWidget(context: Context) { val widgetManager = AppWidgetManager.getInstance(context) val widgetComponent = ComponentName(context, TheWidget::class.java) @@ -103,94 +60,6 @@ object Util { context.sendBroadcast(update) } - fun getWeather(context: Context) { - if (!checkGrantedPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)) { - return - } - val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - - val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager - getCurrentWeather(context, locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)) - - locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f, object: LocationListener { - override fun onLocationChanged(location: Location) { - locationManager.removeUpdates(this) - getCurrentWeather(context, location) - } - - @SuppressLint("ApplySharedPref") - override fun onProviderDisabled(p0: String?) { - SP.edit() - .remove(Constants.PREF_WEATHER_TEMP) - .remove(Constants.PREF_WEATHER_ICON) - .commit() - } - - @SuppressLint("ApplySharedPref") - override fun onProviderEnabled(p0: String?) { - SP.edit() - .remove(Constants.PREF_WEATHER_TEMP) - .remove(Constants.PREF_WEATHER_ICON) - .commit() - } - - override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) { - } - }) - } - - @SuppressLint("ApplySharedPref") - fun getCurrentWeather(context: Context, location: Location) { - val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - - try { - val config = WeatherConfig() - config.unitSystem = if (SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F").equals("C")) WeatherConfig.UNIT_SYSTEM.M else WeatherConfig.UNIT_SYSTEM.I - config.lang = "en" // If you want to use english - config.maxResult = 1 // Max number of cities retrieved - config.numDays = 1 // Max num of days in the forecast - config.ApiKey = "43e744ad8ff91b09ea62dbc7d0e7c1dd"; - - val client = WeatherClient.ClientBuilder().attach(context) - .httpClient(com.survivingwithandroid.weather.lib.client.volley.WeatherClientDefault::class.java) - .provider(OpenweathermapProviderType()) - .config(config) - .build() - - client.getCurrentCondition(WeatherRequest(location.longitude, location.latitude), object : WeatherClient.WeatherEventListener { - @SuppressLint("ApplySharedPref") - override fun onWeatherRetrieved(currentWeather: CurrentWeather) { - SP.edit() - .putFloat(Constants.PREF_WEATHER_TEMP, currentWeather.weather.temperature.temp) - .putString(Constants.PREF_WEATHER_ICON, currentWeather.weather.currentCondition.icon) - .commit() - updateWidget(context) - } - - @SuppressLint("ApplySharedPref") - override fun onWeatherError(e: WeatherLibException?) { - SP.edit() - .remove(Constants.PREF_WEATHER_TEMP) - .remove(Constants.PREF_WEATHER_ICON) - .commit() - } - - @SuppressLint("ApplySharedPref") - override fun onConnectionError(throwable: Throwable?) { - SP.edit() - .remove(Constants.PREF_WEATHER_TEMP) - .remove(Constants.PREF_WEATHER_ICON) - .commit() - } - }) - } catch (t: Exception) { - SP.edit() - .remove(Constants.PREF_WEATHER_TEMP) - .remove(Constants.PREF_WEATHER_ICON) - .commit() - } - } - fun showNotification(context: Context) { val mNotificationManager: NotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager; @@ -242,68 +111,6 @@ object Util { } } - fun getWeatherIconResource(icon: String): Int { - when (icon) { - "01d" -> { - return R.drawable.clear_day - } - "02d" -> { - return R.drawable.partly_cloudy - } - "03d" -> { - return R.drawable.mostly_cloudy - } - "04d" -> { - return R.drawable.cloudy_weather - } - "09d" -> { - return R.drawable.storm_weather_day - } - "10d" -> { - return R.drawable.rainy_day - } - "11d" -> { - return R.drawable.thunder_day - } - "13d" -> { - return R.drawable.snow_day - } - "50d" -> { - return R.drawable.haze_day - } - "01n" -> { - return R.drawable.clear_night - } - "02n" -> { - return R.drawable.partly_cloudy_night - } - "03n" -> { - return R.drawable.mostly_cloudy_night - } - "04n" -> { - return R.drawable.cloudy_weather - } - "09n" -> { - return R.drawable.storm_weather_night - } - "10n" -> { - return R.drawable.rainy_night - } - "11n" -> { - return R.drawable.thunder_night - } - "13n" -> { - return R.drawable.snow_night - } - "50n" -> { - return R.drawable.haze_night - } - else -> { - return -1 - } - } - } - fun getCurrentWallpaper(context: Context): Drawable { return WallpaperManager.getInstance(context).getDrawable(); } diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt new file mode 100644 index 0000000..a6f8473 --- /dev/null +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt @@ -0,0 +1,159 @@ +package com.tommasoberlose.anotherwidget.util + +import android.Manifest +import android.annotation.SuppressLint +import android.content.Context +import android.content.SharedPreferences +import android.location.Location +import android.location.LocationListener +import android.location.LocationManager +import android.os.Bundle +import android.preference.PreferenceManager +import com.survivingwithandroid.weather.lib.WeatherClient +import com.survivingwithandroid.weather.lib.WeatherConfig +import com.survivingwithandroid.weather.lib.exception.WeatherLibException +import com.survivingwithandroid.weather.lib.model.CurrentWeather +import com.survivingwithandroid.weather.lib.provider.openweathermap.OpenweathermapProviderType +import com.survivingwithandroid.weather.lib.request.WeatherRequest +import com.tommasoberlose.anotherwidget.R +import com.tommasoberlose.anotherwidget.`object`.Constants + +/** + * Created by tommaso on 08/10/17. + */ + +object WeatherUtil { + + + fun getWeather(context: Context) { + if (!Util.checkGrantedPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)) { + return + } + val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + + val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager + getCurrentWeather(context, locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)) + + locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f, object: LocationListener { + override fun onLocationChanged(location: Location) { + locationManager.removeUpdates(this) + getCurrentWeather(context, location) + } + + @SuppressLint("ApplySharedPref") + override fun onProviderDisabled(p0: String?) { + } + + @SuppressLint("ApplySharedPref") + override fun onProviderEnabled(p0: String?) { + } + + override fun onStatusChanged(p0: String?, p1: Int, p2: Bundle?) { + } + }) + } + + @SuppressLint("ApplySharedPref") + fun getCurrentWeather(context: Context, location: Location?) { + if (location != null) { + val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + + try { + val config = WeatherConfig() + config.unitSystem = if (SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F").equals("C")) WeatherConfig.UNIT_SYSTEM.M else WeatherConfig.UNIT_SYSTEM.I + config.lang = "en" // If you want to use english + config.maxResult = 1 // Max number of cities retrieved + config.numDays = 1 // Max num of days in the forecast + config.ApiKey = "43e744ad8ff91b09ea62dbc7d0e7c1dd"; + + val client = WeatherClient.ClientBuilder().attach(context) + .httpClient(com.survivingwithandroid.weather.lib.client.volley.WeatherClientDefault::class.java) + .provider(OpenweathermapProviderType()) + .config(config) + .build() + + client.getCurrentCondition(WeatherRequest(location.longitude, location.latitude), object : WeatherClient.WeatherEventListener { + @SuppressLint("ApplySharedPref") + override fun onWeatherRetrieved(currentWeather: CurrentWeather) { + SP.edit() + .putFloat(Constants.PREF_WEATHER_TEMP, currentWeather.weather.temperature.temp) + .putString(Constants.PREF_WEATHER_ICON, currentWeather.weather.currentCondition.icon) + .commit() + Util.updateWidget(context) + } + + @SuppressLint("ApplySharedPref") + override fun onWeatherError(e: WeatherLibException?) { + } + + @SuppressLint("ApplySharedPref") + override fun onConnectionError(throwable: Throwable?) { + } + }) + } catch (t: Exception) { + } + } + } + + fun getWeatherIconResource(icon: String): Int { + when (icon) { + "01d" -> { + return R.drawable.clear_day + } + "02d" -> { + return R.drawable.partly_cloudy + } + "03d" -> { + return R.drawable.mostly_cloudy + } + "04d" -> { + return R.drawable.cloudy_weather + } + "09d" -> { + return R.drawable.storm_weather_day + } + "10d" -> { + return R.drawable.rainy_day + } + "11d" -> { + return R.drawable.thunder_day + } + "13d" -> { + return R.drawable.snow_day + } + "50d" -> { + return R.drawable.haze_day + } + "01n" -> { + return R.drawable.clear_night + } + "02n" -> { + return R.drawable.partly_cloudy_night + } + "03n" -> { + return R.drawable.mostly_cloudy_night + } + "04n" -> { + return R.drawable.cloudy_weather + } + "09n" -> { + return R.drawable.storm_weather_night + } + "10n" -> { + return R.drawable.rainy_night + } + "11n" -> { + return R.drawable.thunder_night + } + "13n" -> { + return R.drawable.snow_night + } + "50n" -> { + return R.drawable.haze_night + } + else -> { + return -1 + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index fa50c67..db38dc8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -85,6 +85,55 @@ android:id="@+id/temp_unit" style="@style/AnotherWidget.Settings.Subtitle"/> + + + + + + + + diff --git a/app/src/main/res/layout/the_widget.xml b/app/src/main/res/layout/the_widget.xml index 6ee3358..d7c5e4f 100644 --- a/app/src/main/res/layout/the_widget.xml +++ b/app/src/main/res/layout/the_widget.xml @@ -33,8 +33,7 @@ android:layout_height="24dp" android:id="@+id/empty_weather_icon" android:layout_marginStart="4dp" - android:layout_marginEnd="4dp" - android:src="@drawable/clear_night"/> + android:layout_marginEnd="4dp"/> Progetto Unità di Misura Modifica l\'unità di misura della temperatura + Filtra Eventi + Scegli quali calendari vedere + Eventi Giornalieri + Visibili + Non Visibili \ 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 615c17a..402f5d1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -25,4 +25,9 @@ Choose the unit of temperature measurement °F - Fahrenheit °C - Celsius + Filter Events + Change calendar visibility + All Day Events + Visible + Not Visible diff --git a/app/src/main/res/xml/the_widget_info.xml b/app/src/main/res/xml/the_widget_info.xml index 41125d0..6b20927 100644 --- a/app/src/main/res/xml/the_widget_info.xml +++ b/app/src/main/res/xml/the_widget_info.xml @@ -2,9 +2,11 @@ \ No newline at end of file