From a82677edc4cf73c185b412e48a8399b06da654ca Mon Sep 17 00:00:00 2001 From: Jaculabilis Date: Sat, 15 Oct 2016 13:32:30 -0500 Subject: [PATCH] Initial widget commit? --- app/src/main/AndroidManifest.xml | 16 ++- .../jaeil/microframe}/FrameWidget.java | 17 +++- .../FrameWidgetConfigureActivity.java | 99 +++++++++++++++++++ .../horse/jaeil/microframe/MainActivity.java | 13 --- app/src/main/res/layout/activity_main.xml | 18 ---- app/src/main/res/layout/frame_widget.xml | 2 +- .../res/layout/frame_widget_configure.xml | 27 +++++ app/src/main/res/values-w820dp/dimens.xml | 6 -- app/src/main/res/values/dimens.xml | 7 +- app/src/main/res/values/strings.xml | 6 +- app/src/main/res/xml/frame_widget_info.xml | 1 + 11 files changed, 153 insertions(+), 59 deletions(-) rename app/src/main/java/{layout => horse/jaeil/microframe}/FrameWidget.java (67%) create mode 100644 app/src/main/java/horse/jaeil/microframe/FrameWidgetConfigureActivity.java delete mode 100644 app/src/main/java/horse/jaeil/microframe/MainActivity.java delete mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/frame_widget_configure.xml delete mode 100644 app/src/main/res/values-w820dp/dimens.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index de70ae2..c360fcf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,15 +8,7 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> - - - - - - - - - + @@ -25,6 +17,12 @@ android:name="android.appwidget.provider" android:resource="@xml/frame_widget_info" /> + + + + + + \ No newline at end of file diff --git a/app/src/main/java/layout/FrameWidget.java b/app/src/main/java/horse/jaeil/microframe/FrameWidget.java similarity index 67% rename from app/src/main/java/layout/FrameWidget.java rename to app/src/main/java/horse/jaeil/microframe/FrameWidget.java index c751670..d04d353 100644 --- a/app/src/main/java/layout/FrameWidget.java +++ b/app/src/main/java/horse/jaeil/microframe/FrameWidget.java @@ -1,24 +1,23 @@ -package layout; +package horse.jaeil.microframe; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.widget.RemoteViews; -import horse.jaeil.microframe.R; - /** * Implementation of App Widget functionality. + * App Widget Configuration implemented in {@link FrameWidgetConfigureActivity FrameWidgetConfigureActivity} */ public class FrameWidget extends AppWidgetProvider { static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { - CharSequence widgetText = context.getString(R.string.appwidget_text); + CharSequence widgetText = FrameWidgetConfigureActivity.loadTitlePref(context, appWidgetId); // Construct the RemoteViews object RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.frame_widget); -// views.setTextViewText(R.id.appwidget_text, widgetText); + views.setTextViewText(R.id.appwidget_text, widgetText); // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); @@ -32,6 +31,14 @@ public class FrameWidget extends AppWidgetProvider { } } + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + // When the user deletes the widget, delete the preference associated with it. + for (int appWidgetId : appWidgetIds) { + FrameWidgetConfigureActivity.deleteTitlePref(context, appWidgetId); + } + } + @Override public void onEnabled(Context context) { // Enter relevant functionality for when the first widget is created diff --git a/app/src/main/java/horse/jaeil/microframe/FrameWidgetConfigureActivity.java b/app/src/main/java/horse/jaeil/microframe/FrameWidgetConfigureActivity.java new file mode 100644 index 0000000..10eb090 --- /dev/null +++ b/app/src/main/java/horse/jaeil/microframe/FrameWidgetConfigureActivity.java @@ -0,0 +1,99 @@ +package horse.jaeil.microframe; + +import android.app.Activity; +import android.appwidget.AppWidgetManager; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.View; +import android.widget.EditText; + +/** + * The configuration screen for the {@link FrameWidget FrameWidget} AppWidget. + */ +public class FrameWidgetConfigureActivity extends Activity { + + private static final String PREFS_NAME = "horse.jaeil.microframe.FrameWidget"; + private static final String PREF_PREFIX_KEY = "appwidget_"; + int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + EditText mAppWidgetText; + View.OnClickListener mOnClickListener = new View.OnClickListener() { + public void onClick(View v) { + final Context context = FrameWidgetConfigureActivity.this; + + // When the button is clicked, store the string locally + String widgetText = mAppWidgetText.getText().toString(); + saveTitlePref(context, mAppWidgetId, widgetText); + + // It is the responsibility of the configuration activity to update the app widget + AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); + FrameWidget.updateAppWidget(context, appWidgetManager, mAppWidgetId); + + // Make sure we pass back the original appWidgetId + Intent resultValue = new Intent(); + resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); + setResult(RESULT_OK, resultValue); + finish(); + } + }; + + public FrameWidgetConfigureActivity() { + super(); + } + + // Write the prefix to the SharedPreferences object for this widget + static void saveTitlePref(Context context, int appWidgetId, String text) { + SharedPreferences.Editor 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 + static String loadTitlePref(Context context, int appWidgetId) { + SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); + String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null); + if (titleValue != null) { + return titleValue; + } else { + return context.getString(R.string.appwidget_text); + } + } + + static void deleteTitlePref(Context context, int appWidgetId) { + SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit(); + prefs.remove(PREF_PREFIX_KEY + appWidgetId); + prefs.apply(); + } + + @Override + public void onCreate(Bundle icicle) { + 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); + + setContentView(R.layout.frame_widget_configure); + mAppWidgetText = (EditText) findViewById(R.id.appwidget_text); + findViewById(R.id.add_button).setOnClickListener(mOnClickListener); + + // Find the widget id from the intent. + Intent intent = getIntent(); + Bundle extras = intent.getExtras(); + if (extras != null) { + mAppWidgetId = 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 (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { + finish(); + return; + } + + mAppWidgetText.setText(loadTitlePref(FrameWidgetConfigureActivity.this, mAppWidgetId)); + } +} + diff --git a/app/src/main/java/horse/jaeil/microframe/MainActivity.java b/app/src/main/java/horse/jaeil/microframe/MainActivity.java deleted file mode 100644 index 778727e..0000000 --- a/app/src/main/java/horse/jaeil/microframe/MainActivity.java +++ /dev/null @@ -1,13 +0,0 @@ -package horse.jaeil.microframe; - -import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; - -public class MainActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - } -} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml deleted file mode 100644 index 5675ebd..0000000 --- a/app/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - diff --git a/app/src/main/res/layout/frame_widget.xml b/app/src/main/res/layout/frame_widget.xml index ddf7ad0..bdd13ce 100644 --- a/app/src/main/res/layout/frame_widget.xml +++ b/app/src/main/res/layout/frame_widget.xml @@ -14,7 +14,7 @@ android:background="#09C" android:contentDescription="@string/appwidget_text" android:text="@string/appwidget_text" - android:textColor="#ff0000" + android:textColor="#ffffff" android:textSize="24sp" android:textStyle="bold|italic" /> diff --git a/app/src/main/res/layout/frame_widget_configure.xml b/app/src/main/res/layout/frame_widget_configure.xml new file mode 100644 index 0000000..b257147 --- /dev/null +++ b/app/src/main/res/layout/frame_widget_configure.xml @@ -0,0 +1,27 @@ + + + + + + + +