diff --git a/app/build.gradle b/app/build.gradle index c28c3cd..74b844a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,6 +17,7 @@ import groovy.xml.MarkupBuilder apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' def twaManifest = [ applicationId: 'website.christine.xesite', @@ -153,6 +154,9 @@ android { lintOptions { checkReleaseBuilds false } + buildFeatures { + viewBinding true + } } task generateShorcutsFile { @@ -202,5 +206,6 @@ dependencies { implementation 'com.google.androidbrowserhelper:locationdelegation:1.0.0' implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.2.0' - + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'com.android.volley:volley:1.2.0' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 11d1cd4..cca6f3b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,48 +1,39 @@ - - - + + + + + + + + + + + + + + - - @@ -52,69 +43,56 @@ android:name="android.support.customtabs.trusted.MANAGE_SPACE_URL" android:value="@string/launchUrl" /> - - - - - - - - - - - - - + - @@ -126,9 +104,7 @@ android:scheme="https" /> - - @@ -147,14 +123,14 @@ android:name=".DelegationService" android:enabled="@bool/enableNotification" android:exported="@bool/enableNotification"> - + - - + + \ No newline at end of file diff --git a/app/src/main/java/website/christine/xesite/CurrentPostWidget.kt b/app/src/main/java/website/christine/xesite/CurrentPostWidget.kt new file mode 100644 index 0000000..f3f1bd2 --- /dev/null +++ b/app/src/main/java/website/christine/xesite/CurrentPostWidget.kt @@ -0,0 +1,44 @@ +package website.christine.xesite + +import android.appwidget.AppWidgetManager +import android.appwidget.AppWidgetProvider +import android.content.Context +import android.widget.RemoteViews + +/** + * Implementation of App Widget functionality. + * App Widget Configuration implemented in [CurrentPostWidgetConfigureActivity] + */ +class CurrentPostWidget : AppWidgetProvider() { + override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { + // There may be multiple widgets active, so update all of them + for (appWidgetId in appWidgetIds) { + updateAppWidget(context, appWidgetManager, appWidgetId) + } + } + + override fun onDeleted(context: Context, appWidgetIds: IntArray) { + // When the user deletes the widget, delete the preference associated with it. + for (appWidgetId in appWidgetIds) { + deleteTitlePref(context, appWidgetId) + } + } + + override fun onEnabled(context: Context) { + // Enter relevant functionality for when the first widget is created + } + + override fun onDisabled(context: Context) { + // Enter relevant functionality for when the last widget is disabled + } +} + +internal fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) { + val widgetText = loadTitlePref(context, appWidgetId) + // Construct the RemoteViews object + val views = RemoteViews(context.packageName, R.layout.current_post_widget) + views.setTextViewText(R.id.appwidget_text, widgetText) + + // Instruct the widget manager to update the widget + appWidgetManager.updateAppWidget(appWidgetId, views) +} \ No newline at end of file diff --git a/app/src/main/java/website/christine/xesite/CurrentPostWidgetConfigureActivity.kt b/app/src/main/java/website/christine/xesite/CurrentPostWidgetConfigureActivity.kt new file mode 100644 index 0000000..c55b7a5 --- /dev/null +++ b/app/src/main/java/website/christine/xesite/CurrentPostWidgetConfigureActivity.kt @@ -0,0 +1,103 @@ +package website.christine.xesite + +import android.app.Activity +import android.appwidget.AppWidgetManager +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.view.View +import android.widget.EditText +import website.christine.xesite.databinding.CurrentPostWidgetConfigureBinding + +/** + * The configuration screen for the [CurrentPostWidget] AppWidget. + */ +class CurrentPostWidgetConfigureActivity : Activity() { + private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID + private lateinit var appWidgetText: EditText + private var onClickListener = View.OnClickListener { + val context = this@CurrentPostWidgetConfigureActivity + + // When the button is clicked, store the string locally + val widgetText = appWidgetText.text.toString() + saveTitlePref(context, appWidgetId, widgetText) + + // It is the responsibility of the configuration activity to update the app widget + val appWidgetManager = AppWidgetManager.getInstance(context) + updateAppWidget(context, appWidgetManager, appWidgetId) + + // Make sure we pass back the original appWidgetId + val resultValue = Intent() + resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) + setResult(RESULT_OK, resultValue) + finish() + } + private lateinit var binding: CurrentPostWidgetConfigureBinding + + public override fun onCreate(icicle: Bundle?) { + super.onCreate(icicle) + + // Set the result to CANCELED. This will cause the widget host to cancel + // out of the widget placement if the user presses the back button. + setResult(RESULT_CANCELED) + + binding = CurrentPostWidgetConfigureBinding.inflate(layoutInflater) + setContentView(binding.root) + + appWidgetText = binding.appwidgetText as EditText + binding.addButton.setOnClickListener(onClickListener) + + // Find the widget id from the intent. + val intent = intent + val extras = intent.extras + if (extras != null) { + appWidgetId = extras.getInt( + AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID) + } + + // If this activity was started with an intent without an app widget ID, finish with an error. + if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { + finish() + return + } + + appWidgetText.setText(loadTitlePref(this@CurrentPostWidgetConfigureActivity, appWidgetId)) + } + +} + +private const val PREFS_NAME = "website.christine.xesite.CurrentPostWidget" +private const val PREF_PREFIX_KEY = "appwidget_" + +internal fun savePref(ctx: Context, key: String, value: String) { + val prefs = ctx.getSharedPreferences(PREFS_NAME, 0).edit() + prefs.putString(PREF_PREFIX_KEY + key, value) + prefs.apply() +} + +internal fun loadPref(ctx: Context, key: String, default: String): String { + val prefs = ctx.getSharedPreferences(PREFS_NAME, 0) + val titleValue = prefs.getString(PREF_PREFIX_KEY + key, null) + return titleValue ?: default +} + +// Write the prefix to the SharedPreferences object for this widget +internal fun saveTitlePref(context: Context, appWidgetId: Int, text: String) { + val prefs = context.getSharedPreferences(PREFS_NAME, 0).edit() + prefs.putString(PREF_PREFIX_KEY + appWidgetId, text) + prefs.apply() +} + +// Read the prefix from the SharedPreferences object for this widget. +// If there is no preference saved, get the default from a resource +internal fun loadTitlePref(context: Context, appWidgetId: Int): String { + val prefs = context.getSharedPreferences(PREFS_NAME, 0) + val titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null) + return titleValue ?: context.getString(R.string.appwidget_text) +} + +internal fun deleteTitlePref(context: Context, appWidgetId: Int) { + val prefs = context.getSharedPreferences(PREFS_NAME, 0).edit() + prefs.remove(PREF_PREFIX_KEY + appWidgetId) + prefs.apply() +} \ No newline at end of file diff --git a/app/src/main/res/drawable-nodpi/example_appwidget_preview.png b/app/src/main/res/drawable-nodpi/example_appwidget_preview.png new file mode 100644 index 0000000..894b069 Binary files /dev/null and b/app/src/main/res/drawable-nodpi/example_appwidget_preview.png differ diff --git a/app/src/main/res/layout/current_post_widget.xml b/app/src/main/res/layout/current_post_widget.xml new file mode 100644 index 0000000..fdd7faa --- /dev/null +++ b/app/src/main/res/layout/current_post_widget.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/current_post_widget_configure.xml b/app/src/main/res/layout/current_post_widget_configure.xml new file mode 100644 index 0000000..0ac5619 --- /dev/null +++ b/app/src/main/res/layout/current_post_widget_configure.xml @@ -0,0 +1,34 @@ + + + + + + + +