diff --git a/app/src/main/java/horse/jaeil/microframe/FrameWidget.java b/app/src/main/java/horse/jaeil/microframe/FrameWidget.java index 27b9752..c5aa083 100644 --- a/app/src/main/java/horse/jaeil/microframe/FrameWidget.java +++ b/app/src/main/java/horse/jaeil/microframe/FrameWidget.java @@ -3,6 +3,8 @@ package horse.jaeil.microframe; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; +import android.net.Uri; +import android.util.Log; import android.widget.RemoteViews; /** @@ -10,14 +12,23 @@ import android.widget.RemoteViews; * App Widget Configuration implemented in {@link FrameWidgetConfigureActivity FrameWidgetConfigureActivity} */ public class FrameWidget extends AppWidgetProvider { + private static final String TAG = "FrameWidget"; static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { -// CharSequence widgetText = FrameWidgetConfigureActivity.loadTitlePref(context, appWidgetId); - // Construct the RemoteViews object + String uriString = FrameWidgetConfigureActivity.loadImgRef(context, appWidgetId); + Log.d(TAG, "Widget updating with uriString=" + uriString); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.frame_widget); -// views.setTextViewText(R.id.appwidget_text, widgetText); + // Depending on the URI string, set the widget image + if (!uriString.equals("")) { + Uri uri = Uri.parse(uriString); + views + .setImageViewUri(R.id.frameImage, uri); + } else { + views + .setImageViewResource(R.id.frameImage, R.drawable.frame_default); + } // Instruct the widget manager to update the widget appWidgetManager.updateAppWidget(appWidgetId, views); @@ -35,7 +46,7 @@ public class FrameWidget extends AppWidgetProvider { 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); + FrameWidgetConfigureActivity.deleteImgRef(context, appWidgetId); } } diff --git a/app/src/main/java/horse/jaeil/microframe/FrameWidgetConfigureActivity.java b/app/src/main/java/horse/jaeil/microframe/FrameWidgetConfigureActivity.java index 0d924e7..65f8c1a 100644 --- a/app/src/main/java/horse/jaeil/microframe/FrameWidgetConfigureActivity.java +++ b/app/src/main/java/horse/jaeil/microframe/FrameWidgetConfigureActivity.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.appwidget.AppWidgetManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; @@ -12,17 +13,19 @@ import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; +import android.widget.TextView; import java.io.FileNotFoundException; import java.io.InputStream; +import java.net.URI; /** * 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_"; + private static final String PREFS_NAME = "horse.jaeil.microframe.FrameWidget"; + private static final String PREF_PREFIX_KEY = "appwidget_"; private static final String TAG = "FrameWidgetConfigure"; private static final int REQUEST_IMAGE_GET = 1; int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; @@ -30,27 +33,30 @@ public class FrameWidgetConfigureActivity extends Activity { public void onClick(View v) { final Context context = FrameWidgetConfigureActivity.this; - // Request an image from any image provider + // Request an image loaded on the device from any image provider Intent intent = new Intent(Intent.ACTION_GET_CONTENT) .putExtra(Intent.EXTRA_LOCAL_ONLY, true) .addCategory(Intent.CATEGORY_OPENABLE) .setType("image/*"); if (intent.resolveActivity(context.getPackageManager()) != null) { startActivityForResult(intent, REQUEST_IMAGE_GET); - Log.d(TAG, "Started selector activity"); } - -// // 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); } }; View.OnClickListener mFinishClickListener = new View.OnClickListener() { public void onClick(View v) { + final Context context = FrameWidgetConfigureActivity.this; + + // Get the URI of the preview image + TextView textView = (TextView) findViewById(R.id.previewUri); + String uriString = textView.getText().toString(); + Log.d(TAG, "Finish clicked with URI " + uriString); + + // Send an update to the widget so it can load the image + saveImgRef(context, mAppWidgetId, uriString); + AppWidgetManager awm = AppWidgetManager.getInstance(context); + FrameWidget.updateAppWidget(context, awm, mAppWidgetId); + // Make sure we pass back the original appWidgetId Intent resultValue = new Intent(); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); @@ -65,56 +71,52 @@ public class FrameWidgetConfigureActivity extends Activity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - Log.d(TAG, "Received Activity result"); - if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) { -// Bitmap thumbnail = data.getParcelableExtra("data"); + // Parse the URI into a drawable Uri fullPhotoUri = data.getData(); - - Log.i(TAG, fullPhotoUri.toString()); - + Log.i(TAG, "URI selected: " + fullPhotoUri); Drawable drawable; + TextView previewText = (TextView) findViewById(R.id.previewUri); try { InputStream inputStream = getContentResolver().openInputStream(fullPhotoUri); drawable = Drawable.createFromStream(inputStream, fullPhotoUri.toString()); + // If the Drawable was successfully loaded, then store the URI for the widget + previewText.setText(fullPhotoUri.toString()); } catch (FileNotFoundException e) { - Log.e(TAG, "Selected image was not found! Now it's going to crash..."); - drawable = null; + Log.e(TAG, "Selected image not found!"); + drawable = getResources().getDrawable(R.drawable.frame_default, null); + previewText.setText(""); } ImageView preview = (ImageView) findViewById(R.id.previewImageView); -// Drawable drawable = new BitmapDrawable(getResources(), thumbnail); -// drawable = Drawable.createFromPath(fullPhotoUri.getPath()); preview.setImageDrawable(drawable); - - Log.d(TAG, "Set preview image"); } } -// // 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(); -// } + // Write the prefix to the SharedPreferences object for this widget + static void saveImgRef(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); -// } -// } + // Read the prefix from the SharedPreferences object for this widget. + // If there is no preference saved, get the default from a resource + static String loadImgRef(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(); -// } + static void deleteImgRef(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) { @@ -139,10 +141,8 @@ public class FrameWidgetConfigureActivity extends Activity { // 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; +// return; } - -// mAppWidgetText.setText(loadTitlePref(FrameWidgetConfigureActivity.this, mAppWidgetId)); } } diff --git a/app/src/main/res/drawable-nodpi/frame_default.png b/app/src/main/res/drawable-nodpi/frame_default.png new file mode 100644 index 0000000..86f91b9 Binary files /dev/null and b/app/src/main/res/drawable-nodpi/frame_default.png differ diff --git a/app/src/main/res/layout/frame_widget.xml b/app/src/main/res/layout/frame_widget.xml index ac65b1d..a2e64b8 100644 --- a/app/src/main/res/layout/frame_widget.xml +++ b/app/src/main/res/layout/frame_widget.xml @@ -6,11 +6,13 @@ android:padding="@dimen/widget_margin"> + android:layout_centerVertical="true" + android:src="@drawable/frame_default" /> \ No newline at end of file diff --git a/app/src/main/res/layout/frame_widget_configure.xml b/app/src/main/res/layout/frame_widget_configure.xml index 58eee45..4d62f39 100644 --- a/app/src/main/res/layout/frame_widget_configure.xml +++ b/app/src/main/res/layout/frame_widget_configure.xml @@ -24,9 +24,15 @@ android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_gravity="center_horizontal" - android:src="@drawable/example_appwidget_preview" + android:src="@drawable/frame_default" tools:ignore="ContentDescription" /> + + +