Update UI, Update project structure, bug fixes
This commit is contained in:
parent
37920e2991
commit
f62f9dc3bd
@ -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"
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":3},"path":"app-release.apk","properties":{"packageId":"com.tommasoberlose.anotherwidget","split":"","minSdkVersion":"19"}}]
|
||||
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":5},"path":"app-release.apk","properties":{"packageId":"com.tommasoberlose.anotherwidget","split":"","minSdkVersion":"19"}}]
|
@ -36,7 +36,7 @@
|
||||
android:resource="@xml/the_widget_info" />
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".util.NewCalendarEventReceiver"
|
||||
android:name=".receiver.NewCalendarEventReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
@ -47,7 +47,7 @@
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".util.UpdatesReceiver"
|
||||
android:name=".receiver.UpdatesReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
@ -55,7 +55,7 @@
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver
|
||||
android:name=".util.WeatherReceiver"
|
||||
android:name=".receiver.WeatherReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="false">
|
||||
<intent-filter>
|
||||
|
@ -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
|
||||
}
|
||||
}
|
@ -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())
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
@ -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)
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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<Event> {
|
||||
val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
val eventList = ArrayList<Event>()
|
||||
|
||||
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<com.tommasoberlose.anotherwidget.`object`.Calendar> {
|
||||
val calendarList = ArrayList<com.tommasoberlose.anotherwidget.`object`.Calendar>()
|
||||
|
||||
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
|
||||
}
|
||||
}
|
@ -50,49 +50,6 @@ object Util {
|
||||
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
|
||||
fun getNextEvent(context: Context): List<Event> {
|
||||
val eventList = ArrayList<Event>()
|
||||
|
||||
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();
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -85,6 +85,55 @@
|
||||
android:id="@+id/temp_unit"
|
||||
style="@style/AnotherWidget.Settings.Subtitle"/>
|
||||
</LinearLayout>
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingBottom="8dp"
|
||||
android:paddingLeft="32dp"
|
||||
android:paddingRight="32dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:gravity="center_vertical"
|
||||
android:id="@+id/action_filter_calendar"
|
||||
android:visibility="gone"
|
||||
android:orientation="vertical">
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/AnotherWidget.Settings.Title"
|
||||
android:text="@string/settings_filter_calendar_title"/>
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/settings_filter_calendar_subtitle"
|
||||
style="@style/AnotherWidget.Settings.Subtitle"/>
|
||||
</LinearLayout>
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="8dp"
|
||||
android:paddingBottom="8dp"
|
||||
android:paddingLeft="32dp"
|
||||
android:paddingRight="32dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true"
|
||||
android:background="?android:attr/selectableItemBackground"
|
||||
android:gravity="center_vertical"
|
||||
android:id="@+id/action_show_all_day"
|
||||
android:orientation="vertical">
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
style="@style/AnotherWidget.Settings.Title"
|
||||
android:text="@string/settings_all_day_title"/>
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/all_day_label"
|
||||
style="@style/AnotherWidget.Settings.Subtitle"/>
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
|
@ -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"/>
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
|
@ -24,4 +24,9 @@
|
||||
<string name="action_project">Progetto</string>
|
||||
<string name="settings_unit_title">Unità di Misura</string>
|
||||
<string name="settings_unit_subtitle">Modifica l\'unità di misura della temperatura</string>
|
||||
<string name="settings_filter_calendar_title">Filtra Eventi</string>
|
||||
<string name="settings_filter_calendar_subtitle">Scegli quali calendari vedere</string>
|
||||
<string name="settings_all_day_title">Eventi Giornalieri</string>
|
||||
<string name="settings_all_day_subtitle_visible">Visibili</string>
|
||||
<string name="settings_all_day_subtitle_gone">Non Visibili</string>
|
||||
</resources>
|
@ -25,4 +25,9 @@
|
||||
<string name="settings_unit_subtitle">Choose the unit of temperature measurement</string>
|
||||
<string name="fahrenheit" translatable="false">°F - Fahrenheit</string>
|
||||
<string name="celsius" translatable="false">°C - Celsius</string>
|
||||
<string name="settings_filter_calendar_title">Filter Events</string>
|
||||
<string name="settings_filter_calendar_subtitle">Change calendar visibility</string>
|
||||
<string name="settings_all_day_title">All Day Events</string>
|
||||
<string name="settings_all_day_subtitle_visible">Visible</string>
|
||||
<string name="settings_all_day_subtitle_gone">Not Visible</string>
|
||||
</resources>
|
||||
|
@ -2,9 +2,11 @@
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:initialKeyguardLayout="@layout/the_widget"
|
||||
android:initialLayout="@layout/the_widget"
|
||||
android:minHeight="40dp"
|
||||
android:minWidth="250dp"
|
||||
android:minHeight="128dp"
|
||||
android:minWidth="300dp"
|
||||
android:minResizeHeight="40dp"
|
||||
android:minResizeWidth="250dp"
|
||||
android:previewImage="@drawable/widget_preview"
|
||||
android:resizeMode="horizontal"
|
||||
android:resizeMode="vertical|horizontal"
|
||||
android:updatePeriodMillis="60000"
|
||||
android:widgetCategory="home_screen" />
|
Loading…
x
Reference in New Issue
Block a user