Add next alarm, add weather provider personal key

This commit is contained in:
Tommaso Berlose
2017-10-31 18:06:47 +01:00
parent 4cbb5d702f
commit fde3fdfab1
17 changed files with 171 additions and 69 deletions

View File

@ -30,6 +30,7 @@ import android.os.Build
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
@ -121,7 +122,8 @@ class MainActivity : AppCompatActivity() {
if (mAppWidgetId > 0) {
addNewWidget()
} else {
super.onBackPressed()
setResult(Activity.RESULT_OK)
finish()
}
}
@ -260,7 +262,11 @@ class MainActivity : AppCompatActivity() {
next_event_difference_time.visibility = View.GONE
}
if (!e.address.equals("") && SP.getBoolean(Constants.PREF_SHOW_EVENT_LOCATION, false)) {
if (SP.getInt(Constants.PREF_SECOND_ROW_INFORMATION, 0) == 2 && Util.getNextAlarm(this) != null) {
second_row_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_action_alarm))
next_event_date.text = Util.getNextAlarm(this)
} else if (!e.address.equals("") && SP.getInt(Constants.PREF_SECOND_ROW_INFORMATION, 0) == 1) {
second_row_icon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_action_location))
next_event_date.text = e.address
} else {
@ -505,12 +511,16 @@ 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)
second_row_info_label.text = getString(Util.getSecondRowInfoString(SP.getInt(Constants.PREF_SECOND_ROW_INFORMATION, 0)))
action_second_row_info.setOnClickListener {
SP.edit().putInt(Constants.PREF_SECOND_ROW_INFORMATION, when (SP.getInt(Constants.PREF_SECOND_ROW_INFORMATION, 0)) {
0 -> 1
1 -> 2
2 -> 0
else -> 0
}).commit()
updateSettings()
updateAppWidget()
sendBroadcast(Intent(Constants.ACTION_TIME_UPDATE))
}
main_text_size_label.text = String.format("%.0f%s", SP.getFloat(Constants.PREF_TEXT_MAIN_SIZE, 24f), "sp")
@ -645,7 +655,10 @@ class MainActivity : AppCompatActivity() {
label_weather_provider_api_key.text = getString(R.string.provider_google_awareness)
alert_icon.visibility = View.GONE
} else {
if (SP.getString(Constants.PREF_WEATHER_PROVIDER_API_KEY, "") == ("")) {
if (SP.getString(when (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS)) {
Constants.WEATHER_PROVIDER_OPEN_WEATHER -> Constants.PREF_OPEN_WEATHER_API_KEY
else -> Constants.PREF_OPEN_WEATHER_API_KEY
}, "") == ("")) {
label_weather_provider_api_key.text = getString(R.string.settings_weather_provider_api_key_subtitle_not_set)
alert_icon.visibility = View.VISIBLE
} else {

View File

@ -41,7 +41,10 @@ class WeatherProviderActivity : AppCompatActivity() {
action_save.setOnClickListener {
SP.edit()
.putString(Constants.PREF_WEATHER_PROVIDER_API_KEY, api_key.text.toString())
.putString(when (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS)) {
Constants.WEATHER_PROVIDER_OPEN_WEATHER -> Constants.PREF_OPEN_WEATHER_API_KEY
else -> Constants.PREF_OPEN_WEATHER_API_KEY
}, api_key.text.toString())
.commit()
setResult(Activity.RESULT_OK)
finish()
@ -49,7 +52,11 @@ class WeatherProviderActivity : AppCompatActivity() {
api_key.addTextChangedListener(object: TextWatcher {
override fun afterTextChanged(text: Editable?) {
if (text.toString().equals("") || text.toString().equals(SP.getString(Constants.PREF_WEATHER_PROVIDER_API_KEY, ""))) {
if (text.toString().equals("") || text.toString().equals(
SP.getString(when (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS)) {
Constants.WEATHER_PROVIDER_OPEN_WEATHER -> Constants.PREF_OPEN_WEATHER_API_KEY
else -> Constants.PREF_OPEN_WEATHER_API_KEY
}, ""))) {
Util.collapse(button_container)
} else {
Util.expand(button_container)
@ -98,12 +105,18 @@ class WeatherProviderActivity : AppCompatActivity() {
}
Util.collapse(button_container)
api_key.setText(SP.getString(Constants.PREF_WEATHER_PROVIDER_API_KEY, ""))
api_key.setText(SP.getString(when (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS)) {
Constants.WEATHER_PROVIDER_OPEN_WEATHER -> Constants.PREF_OPEN_WEATHER_API_KEY
else -> Constants.PREF_OPEN_WEATHER_API_KEY
}, ""))
}
override fun onBackPressed() {
val SP = PreferenceManager.getDefaultSharedPreferences(this)
if (!SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS).equals(Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) && (api_key.text.toString().equals("") || !api_key.text.toString().equals(SP.getString(Constants.PREF_WEATHER_PROVIDER_API_KEY, "")))) {
if (!SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS).equals(Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS) && (api_key.text.toString().equals("") || !api_key.text.toString().equals(SP.getString(when (SP.getInt(Constants.PREF_WEATHER_PROVIDER, Constants.WEATHER_PROVIDER_GOOGLE_AWARENESS)) {
Constants.WEATHER_PROVIDER_OPEN_WEATHER -> Constants.PREF_OPEN_WEATHER_API_KEY
else -> Constants.PREF_OPEN_WEATHER_API_KEY
}, "")))) {
AlertDialog.Builder(this)
.setMessage(getString(R.string.error_weather_api_key))
.setNegativeButton(android.R.string.cancel, null)

View File

@ -29,6 +29,7 @@ import android.net.Uri
import android.widget.TextClock
import android.widget.TextView
import android.content.ComponentName
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
@ -37,6 +38,7 @@ import android.os.Bundle
import android.support.v4.content.ContextCompat.startActivity
import android.provider.CalendarContract.Events
import android.support.v4.content.ContextCompat
import android.util.DisplayMetrics
import android.util.TypedValue
import kotlinx.android.synthetic.main.the_widget.*
import kotlinx.android.synthetic.main.the_widget.view.*
@ -48,6 +50,8 @@ import kotlinx.android.synthetic.main.the_widget.view.*
class TheWidget : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
Util.updateSettingsByDefault(context)
for (appWidgetId in appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId)
}
@ -73,13 +77,21 @@ class TheWidget : AppWidgetProvider() {
internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager,
appWidgetId: Int) {
val displayMetrics = Resources.getSystem().getDisplayMetrics()
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
val widgetInfo = appWidgetManager.getAppWidgetInfo(appWidgetId)
generateWidgetView(context, appWidgetId, appWidgetManager, width - Util.convertDpToPixel(32f, context).toInt(), widgetInfo.minHeight)
}
fun generateWidgetView(context: Context, appWidgetId: Int, appWidgetManager: AppWidgetManager, w: Int, h: Int) {
var views = RemoteViews(context.packageName, R.layout.the_widget_sans)
var v = View.inflate(context, R.layout.the_widget, null)
v = updateCalendarViewByLayout(context, v)
v = updateLocationViewByLayout(context, v)
v = updateClockViewByLayout(context, v)
views.setImageViewBitmap(R.id.bitmap_container, Util.getBitmapFromView(v))
views.setImageViewBitmap(R.id.bitmap_container, Util.getBitmapFromView(v, w, h))
views = updateCalendarView(context, views, appWidgetId)
views = updateLocationView(context, views, appWidgetId)
@ -104,7 +116,6 @@ class TheWidget : AppWidgetProvider() {
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)
}
@ -138,7 +149,16 @@ class TheWidget : AppWidgetProvider() {
views.setViewVisibility(R.id.next_event_difference_time, View.GONE)
}
if (!e.address.equals("") && SP.getBoolean(Constants.PREF_SHOW_EVENT_LOCATION, false)) {
if (SP.getInt(Constants.PREF_SECOND_ROW_INFORMATION, 0) == 2 && Util.getNextAlarm(context) != null) {
val source = BitmapFactory.decodeResource(context.resources, R.drawable.ic_action_alarm);
val result = Util.changeBitmapColor(source, Util.getFontColor(SP))
views.setImageViewBitmap(R.id.second_row_icon, result)
views.setTextViewText(R.id.next_event_date, Util.getNextAlarm(context))
val clockIntent = PendingIntent.getActivity(context, widgetID, Util.getClockIntent(context), 0)
views.setOnClickPendingIntent(R.id.next_event_date, clockIntent)
} else if (e.address != "" && SP.getInt(Constants.PREF_SECOND_ROW_INFORMATION, 0) == 1) {
val source = BitmapFactory.decodeResource(context.resources, R.drawable.ic_action_location);
val result = Util.changeBitmapColor(source, Util.getFontColor(SP))
@ -257,6 +277,7 @@ class TheWidget : AppWidgetProvider() {
if (SP.getBoolean(Constants.PREF_ITA_FORMAT_DATE, false)) {
dateStringValue = Util.getCapWordString(Constants.itDateFormat.format(now.time))
}
v.empty_date.text = dateStringValue
if (calendarLayout) {
@ -271,7 +292,10 @@ class TheWidget : AppWidgetProvider() {
v.next_event_difference_time.visibility = View.GONE
}
if (!e.address.equals("") && SP.getBoolean(Constants.PREF_SHOW_EVENT_LOCATION, false)) {
if (SP.getInt(Constants.PREF_SECOND_ROW_INFORMATION, 0) == 2 && Util.getNextAlarm(context) != null) {
v.second_row_icon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_action_alarm))
v.next_event_date.text = Util.getNextAlarm(context)
} else if (!e.address.equals("") && SP.getInt(Constants.PREF_SECOND_ROW_INFORMATION, 0) == 1) {
v.second_row_icon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_action_location))
v.next_event_date.text = e.address
} else {