diff --git a/app/build.gradle b/app/build.gradle index 7ff6c26..3bcc82e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -15,7 +15,7 @@ android { applicationId "com.tommasoberlose.anotherwidget" minSdkVersion 19 targetSdkVersion 26 - versionCode 14 + versionCode 15 versionName "1.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } @@ -48,4 +48,6 @@ dependencies { compile 'com.android.support:design:26.1.0' compile 'org.greenrobot:eventbus:3.0.0' 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' } diff --git a/app/release/output.json b/app/release/output.json index 647a792..5616265 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":14},"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":15},"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 9283100..a455131 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,6 +14,11 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> + + + 1000 * 60) { - var time = "" - val hour = TimeUnit.MILLISECONDS.toHours(difference) - if (hour > 0) { - time = hour.toString() + getString(R.string.h_code) + " " - } - val minutes = TimeUnit.MILLISECONDS.toMinutes(difference - hour * 3600 * 1000) - if (minutes > 0) { - time += "" + minutes + getString(R.string.min_code) - } - - next_event.text = String.format("%s %s %s", e.title, getString(R.string.in_code), time) - } else { - next_event.text = String.format("%s", e.title) - } + next_event.text = Util.getDifferenceText(this, e.title, 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) @@ -268,7 +257,9 @@ class MainActivity : AppCompatActivity() { val endCal = Calendar.getInstance() endCal.timeInMillis = e.endDate - if (startCal.get(Calendar.HOUR_OF_DAY) >= endCal.get(Calendar.HOUR_OF_DAY)) { + 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 = "" @@ -455,6 +446,11 @@ class MainActivity : AppCompatActivity() { startActivityForResult(Intent(this, ChooseApplicationActivity::class.java), Constants.WEATHER_APP_REQUEST_CODE) } + event_app_label.text = SP.getString(Constants.PREF_EVENT_APP_NAME, getString(R.string.default_name)) + action_event_app.setOnClickListener { + 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 diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/WeatherProviderActivity.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/WeatherProviderActivity.kt index ec89fb3..861a1f7 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/WeatherProviderActivity.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/activity/WeatherProviderActivity.kt @@ -34,7 +34,9 @@ class WeatherProviderActivity : AppCompatActivity() { val SP = PreferenceManager.getDefaultSharedPreferences(this) action_paste.setOnClickListener { val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager - api_key.setText(clipboard.primaryClip.getItemAt(0).text) + if (clipboard.primaryClip != null && clipboard.primaryClip.itemCount > 0) { + api_key.setText(clipboard.primaryClip.getItemAt(0).text) + } } Util.collapse(button_container) 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 328a31d..06019e5 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 @@ -96,23 +96,7 @@ class TheWidget : AppWidgetProvider() { val e = CalendarUtil.getNextEvent(context) if (e.id != 0) { - val difference = e.startDate - now.timeInMillis - - if (difference > 1000 * 60) { - var time = "" - val hour = TimeUnit.MILLISECONDS.toHours(difference) - if (hour > 0) { - time = hour.toString() + context.getString(R.string.h_code) + " " - } - val minutes = TimeUnit.MILLISECONDS.toMinutes(difference - hour * 3600 * 1000) - if (minutes > 0) { - time += "" + minutes + context.getString(R.string.min_code) - } - - views.setTextViewText(R.id.next_event, String.format("%s %s %s", e.title, context.getString(R.string.in_code), time)) - } else { - views.setTextViewText(R.id.next_event, String.format("%s", e.title)) - } + views.setTextViewText(R.id.next_event, Util.getDifferenceText(context, e.title, 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) @@ -125,7 +109,9 @@ class TheWidget : AppWidgetProvider() { val endCal = Calendar.getInstance() endCal.timeInMillis = e.endDate - if (startCal.get(Calendar.HOUR_OF_DAY) >= endCal.get(Calendar.HOUR_OF_DAY)) { + 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++ } @@ -143,12 +129,8 @@ class TheWidget : AppWidgetProvider() { views.setViewVisibility(R.id.calendar_layout, View.VISIBLE) - val uri = ContentUris.withAppendedId(Events.CONTENT_URI, e.id.toLong()) - val intent = Intent(Intent.ACTION_VIEW) - .setData(uri) - intent.putExtra("beginTime", e.startDate); - intent.putExtra("endTime", e.endDate); - val pIntent = PendingIntent.getActivity(context, widgetID, intent, 0) + + 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/Util.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt index 75326ab..c20ab5b 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/Util.kt @@ -27,6 +27,7 @@ import android.util.TypedValue import android.content.Intent import android.content.ComponentName import android.preference.PreferenceManager +import android.provider.CalendarContract import android.provider.Settings import android.util.Log import android.view.View @@ -35,6 +36,9 @@ import android.view.animation.Transformation import android.widget.LinearLayout import android.widget.Toast import com.tommasoberlose.anotherwidget.`object`.Constants +import com.tommasoberlose.anotherwidget.`object`.Event +import org.joda.time.DateTime +import java.util.concurrent.TimeUnit /** @@ -236,6 +240,32 @@ object Util { } } + fun getEventIntent(context: Context, e: Event): Intent { + val SP = PreferenceManager.getDefaultSharedPreferences(context) + if (SP.getString(Constants.PREF_EVENT_APP_PACKAGE, "").equals("")) { + val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, e.id.toLong()) + val intent = Intent(Intent.ACTION_VIEW) + .setData(uri) + intent.putExtra("beginTime", e.startDate); + intent.putExtra("endTime", e.endDate); + return intent + } else { + val pm: PackageManager = context.packageManager + return try { + val intent: Intent = pm.getLaunchIntentForPackage(SP.getString(Constants.PREF_CALENDAR_APP_PACKAGE, "")) + intent.addCategory(Intent.CATEGORY_LAUNCHER) + intent + } catch (ex: Exception) { + val uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, e.id.toLong()) + val intent = Intent(Intent.ACTION_VIEW) + .setData(uri) + intent.putExtra("beginTime", e.startDate); + intent.putExtra("endTime", e.endDate); + intent + } + } + } + fun getCapWordString(text: String): String { return try { val ar = text.split(" ") @@ -360,4 +390,48 @@ object Util { fun getEmojiByUnicode(unicode: Int): String { return String(Character.toChars(unicode)) } + + fun getDifferenceText(context: Context, title: String, 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) + } else if (difference < 1000 * 60) { + val minutes = TimeUnit.MILLISECONDS.toMinutes(difference) + var time = "" + if (minutes > 0) { + time += "" + minutes + context.getString(R.string.min_code) + } + + return String.format("%s %s %s", title, context.getString(R.string.in_code), time) + } else if (difference < 1000 * 60 * 6) { + val hour = TimeUnit.MILLISECONDS.toHours(difference) + var time = "" + if (hour > 0) { + time = if (hour > 1) { + hour.toString() + context.getString(R.string.hs_code) + " " + } else { + hour.toString() + context.getString(R.string.h_code) + " " + } + } + val minutes = TimeUnit.MILLISECONDS.toMinutes(difference - hour * 3600 * 1000) + if (minutes > 0) { + time += "" + minutes + context.getString(R.string.min_code) + } + + return String.format("%s %s %s", title, 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)) + } else if (eventDate.dayOfYear == nowDate.dayOfYear) { + return String.format("%s %s", title, 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)) + } + + } + } 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 e14bffa..3505b6e 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/util/WeatherUtil.kt @@ -21,6 +21,11 @@ import android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS import android.content.Intent import android.location.LocationManager import android.util.Log +import com.google.android.gms.awareness.Awareness +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 /** @@ -32,8 +37,12 @@ object WeatherUtil { fun updateWeather(context: Context) { Util.showLocationNotification(context, false) val SP: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) + if (SP.getString(Constants.PREF_CUSTOM_LOCATION_ADD, "").equals("") || SP.getString(Constants.PREF_CUSTOM_LOCATION_LAT, "").equals("") || SP.getString(Constants.PREF_CUSTOM_LOCATION_LON, "").equals("")) { + newWeatherProvider(context) + return +/* if (!Util.checkGrantedPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) { return } @@ -50,56 +59,44 @@ object WeatherUtil { } catch (ex: Exception) { } - if (!gpsEnabled && !networkEnabled) { + if (!gpsEnabled && !networkEnabled && SP.getBoolean(Constants.PREF_SHOW_WEATHER, true)) { Util.showLocationNotification(context, true) } else { if (gpsEnabled) { - getCurrentWeather(context, locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)) - locationManager.requestLocationUpdates(LocationManager.GPS_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?) { - } - }) + val gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) + if (gpsLocation != null) { + getCurrentWeather(context, gpsLocation) + return + } } - if (networkEnabled) { 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?) { - } - }) } - } + }*/ } else { weatherNetworkRequest(context, SP.getString(Constants.PREF_CUSTOM_LOCATION_LAT, "").toDouble(), SP.getString(Constants.PREF_CUSTOM_LOCATION_LON, "").toDouble()) } } + @SuppressLint("ApplySharedPref") + fun newWeatherProvider(context: Context) { + if (!Util.checkGrantedPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)) { + return + } + Awareness.getSnapshotClient(context).weather + .addOnSuccessListener { weatherResponse -> + val weather = weatherResponse.weather + 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_REAL_TEMP_UNIT, SP.getString(Constants.PREF_WEATHER_TEMP_UNIT, "F")) + .commit() + Util.updateWidget(context) + } + + } + @SuppressLint("ApplySharedPref") fun getCurrentWeather(context: Context, location: Location?) { if (location != null) { diff --git a/app/src/main/res/layout/activity_choose_application.xml b/app/src/main/res/layout/activity_choose_application.xml index e8f16a1..c208812 100644 --- a/app/src/main/res/layout/activity_choose_application.xml +++ b/app/src/main/res/layout/activity_choose_application.xml @@ -69,7 +69,7 @@ android:contentDescription="@string/custom_location_gps" android:id="@+id/action_default" android:src="@drawable/ic_action_reset" - android:tint="@android:color/primary_text_light"/> + android:tint="@android:color/black"/> + android:tint="@android:color/black"/> + + + + + android:tint="@android:color/black"/> + android:textColor="@android:color/black"/> \ No newline at end of file diff --git a/app/src/main/res/layout/custom_location_item.xml b/app/src/main/res/layout/custom_location_item.xml index 6d41d9f..732f707 100644 --- a/app/src/main/res/layout/custom_location_item.xml +++ b/app/src/main/res/layout/custom_location_item.xml @@ -5,5 +5,5 @@ android:layout_height="?android:attr/actionBarSize" android:gravity="center_vertical" android:padding="16dp" - android:textColor="@android:color/primary_text_light" + android:textColor="@android:color/black" style="@style/AnotherWidget.Settings.Title"/> \ No newline at end of file diff --git a/app/src/main/res/layout/key_time_wait_layout.xml b/app/src/main/res/layout/key_time_wait_layout.xml index 9a84147..cb6e8d0 100644 --- a/app/src/main/res/layout/key_time_wait_layout.xml +++ b/app/src/main/res/layout/key_time_wait_layout.xml @@ -11,11 +11,11 @@ android:id="@+id/title" style="@style/AnotherWidget.Main.Title" android:textSize="20sp" - android:textColor="@android:color/primary_text_light"/> + android:textColor="@android:color/black"/> + android:textColor="@android:color/black" /> + android:textColor="@android:color/black"/> + android:textColor="@android:color/black"/> + android:textColor="@android:color/black"/> + android:textColor="@android:color/black"/> Concedi l\'accesso alla tua posizione per vedere il meteo. Hai completato la configurazione! Rimani aggiornato. Info - " ore" + " ora" " min" tra Progetto @@ -94,4 +94,6 @@
Però a causa del successo dell\'applicazione, sicuramente oltre ogni mia aspettativa, il provider del meteo non riesce a supportare il numero di richieste che arrivano (a causa dell\'utilizzo di un account gratuito).

Sono costretto a chiedervi di registrarvi su OpenWeather.com; l\'operazione richiederà solo alcuni minuti e dopo non avrete più problemi con la fruizione delle informazioni sul meteo!

Mi dispiace per l\'inconveniente ma continuate a supportarmi!!]]>
Ben Fatto! dieci minuti prima che la tua chiave sia attivata.

Quindi rilassati! Il meteo verrà aggiornato non appena sarà disponibile!]]>
+ " ore" + domani \ 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 0e3833f..8128bca 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -96,4 +96,8 @@ Go to OpenWeather.com 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 + Tap on event opens