Working on getting widget images to load

This commit is contained in:
Jaculabilis 2016-10-15 20:52:01 -05:00
parent 0789bbfdfa
commit 647a392d30
6 changed files with 75 additions and 56 deletions

View File

@ -3,6 +3,8 @@ package horse.jaeil.microframe;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider; import android.appwidget.AppWidgetProvider;
import android.content.Context; import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews; import android.widget.RemoteViews;
/** /**
@ -10,14 +12,23 @@ import android.widget.RemoteViews;
* App Widget Configuration implemented in {@link FrameWidgetConfigureActivity FrameWidgetConfigureActivity} * App Widget Configuration implemented in {@link FrameWidgetConfigureActivity FrameWidgetConfigureActivity}
*/ */
public class FrameWidget extends AppWidgetProvider { public class FrameWidget extends AppWidgetProvider {
private static final String TAG = "FrameWidget";
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) { int appWidgetId) {
// CharSequence widgetText = FrameWidgetConfigureActivity.loadTitlePref(context, appWidgetId); String uriString = FrameWidgetConfigureActivity.loadImgRef(context, appWidgetId);
// Construct the RemoteViews object Log.d(TAG, "Widget updating with uriString=" + uriString);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.frame_widget); 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 // Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views); appWidgetManager.updateAppWidget(appWidgetId, views);
@ -35,7 +46,7 @@ public class FrameWidget extends AppWidgetProvider {
public void onDeleted(Context context, int[] appWidgetIds) { public void onDeleted(Context context, int[] appWidgetIds) {
// When the user deletes the widget, delete the preference associated with it. // When the user deletes the widget, delete the preference associated with it.
for (int appWidgetId : appWidgetIds) { for (int appWidgetId : appWidgetIds) {
// FrameWidgetConfigureActivity.deleteTitlePref(context, appWidgetId); FrameWidgetConfigureActivity.deleteImgRef(context, appWidgetId);
} }
} }

View File

@ -4,6 +4,7 @@ import android.app.Activity;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@ -12,17 +13,19 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
/** /**
* The configuration screen for the {@link FrameWidget FrameWidget} AppWidget. * The configuration screen for the {@link FrameWidget FrameWidget} AppWidget.
*/ */
public class FrameWidgetConfigureActivity extends Activity { public class FrameWidgetConfigureActivity extends Activity {
// private static final String PREFS_NAME = "horse.jaeil.microframe.FrameWidget"; private static final String PREFS_NAME = "horse.jaeil.microframe.FrameWidget";
// private static final String PREF_PREFIX_KEY = "appwidget_"; private static final String PREF_PREFIX_KEY = "appwidget_";
private static final String TAG = "FrameWidgetConfigure"; private static final String TAG = "FrameWidgetConfigure";
private static final int REQUEST_IMAGE_GET = 1; private static final int REQUEST_IMAGE_GET = 1;
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
@ -30,27 +33,30 @@ public class FrameWidgetConfigureActivity extends Activity {
public void onClick(View v) { public void onClick(View v) {
final Context context = FrameWidgetConfigureActivity.this; 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) Intent intent = new Intent(Intent.ACTION_GET_CONTENT)
.putExtra(Intent.EXTRA_LOCAL_ONLY, true) .putExtra(Intent.EXTRA_LOCAL_ONLY, true)
.addCategory(Intent.CATEGORY_OPENABLE) .addCategory(Intent.CATEGORY_OPENABLE)
.setType("image/*"); .setType("image/*");
if (intent.resolveActivity(context.getPackageManager()) != null) { if (intent.resolveActivity(context.getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_GET); 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() { View.OnClickListener mFinishClickListener = new View.OnClickListener() {
public void onClick(View v) { 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 // Make sure we pass back the original appWidgetId
Intent resultValue = new Intent(); Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
@ -65,56 +71,52 @@ public class FrameWidgetConfigureActivity extends Activity {
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "Received Activity result");
if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) { if (requestCode == REQUEST_IMAGE_GET && resultCode == RESULT_OK) {
// Bitmap thumbnail = data.getParcelableExtra("data"); // Parse the URI into a drawable
Uri fullPhotoUri = data.getData(); Uri fullPhotoUri = data.getData();
Log.i(TAG, "URI selected: " + fullPhotoUri);
Log.i(TAG, fullPhotoUri.toString());
Drawable drawable; Drawable drawable;
TextView previewText = (TextView) findViewById(R.id.previewUri);
try { try {
InputStream inputStream = getContentResolver().openInputStream(fullPhotoUri); InputStream inputStream = getContentResolver().openInputStream(fullPhotoUri);
drawable = Drawable.createFromStream(inputStream, fullPhotoUri.toString()); 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) { } catch (FileNotFoundException e) {
Log.e(TAG, "Selected image was not found! Now it's going to crash..."); Log.e(TAG, "Selected image not found!");
drawable = null; drawable = getResources().getDrawable(R.drawable.frame_default, null);
previewText.setText("");
} }
ImageView preview = (ImageView) findViewById(R.id.previewImageView); ImageView preview = (ImageView) findViewById(R.id.previewImageView);
// Drawable drawable = new BitmapDrawable(getResources(), thumbnail);
// drawable = Drawable.createFromPath(fullPhotoUri.getPath());
preview.setImageDrawable(drawable); preview.setImageDrawable(drawable);
Log.d(TAG, "Set preview image");
} }
} }
// // Write the prefix to the SharedPreferences object for this widget // Write the prefix to the SharedPreferences object for this widget
// static void saveTitlePref(Context context, int appWidgetId, String text) { static void saveImgRef(Context context, int appWidgetId, String text) {
// SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit(); SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
// prefs.putString(PREF_PREFIX_KEY + appWidgetId, text); prefs.putString(PREF_PREFIX_KEY + appWidgetId, text);
// prefs.apply(); prefs.apply();
// } }
// // Read the prefix from the SharedPreferences object for this widget. // Read the prefix from the SharedPreferences object for this widget.
// // If there is no preference saved, get the default from a resource // If there is no preference saved, get the default from a resource
// static String loadTitlePref(Context context, int appWidgetId) { static String loadImgRef(Context context, int appWidgetId) {
// SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0); SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
// String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null); String titleValue = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);
// if (titleValue != null) { if (titleValue != null) {
// return titleValue; return titleValue;
// } else { } else {
// return context.getString(R.string.appwidget_text); return "";//context.getString(R.string.appwidget_text);
// } }
// } }
// static void deleteTitlePref(Context context, int appWidgetId) { static void deleteImgRef(Context context, int appWidgetId) {
// SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit(); SharedPreferences.Editor prefs = context.getSharedPreferences(PREFS_NAME, 0).edit();
// prefs.remove(PREF_PREFIX_KEY + appWidgetId); prefs.remove(PREF_PREFIX_KEY + appWidgetId);
// prefs.apply(); prefs.apply();
// } }
@Override @Override
public void onCreate(Bundle icicle) { 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 this activity was started with an intent without an app widget ID, finish with an error.
if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish(); finish();
return; // return;
} }
// mAppWidgetText.setText(loadTitlePref(FrameWidgetConfigureActivity.this, mAppWidgetId));
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

@ -6,11 +6,13 @@
android:padding="@dimen/widget_margin"> android:padding="@dimen/widget_margin">
<ImageView <ImageView
android:id="@+id/frameImage"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#09C" android:background="#09C"
tools:ignore="ContentDescription" tools:ignore="ContentDescription"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_centerVertical="true" /> android:layout_centerVertical="true"
android:src="@drawable/frame_default" />
</RelativeLayout> </RelativeLayout>

View File

@ -24,9 +24,15 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:src="@drawable/example_appwidget_preview" android:src="@drawable/frame_default"
tools:ignore="ContentDescription" /> tools:ignore="ContentDescription" />
<TextView
android:id="@+id/previewUri"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--android:visibility="gone"> /-->
<Button <Button
android:id="@+id/finish_button" android:id="@+id/finish_button"
android:layout_width="match_parent" android:layout_width="match_parent"

View File

@ -2,5 +2,5 @@
<string name="app_name">MicroFrame</string> <string name="app_name">MicroFrame</string>
<string name="select_an_image">Select an Image</string> <string name="select_an_image">Select an Image</string>
<string name="finish">Finish</string> <string name="finish">Finish</string>
<string name="about">Version: 0.2.23\nAuthor: Tim Van Baak</string> <string name="about">Version: 0.3.3\nAuthor: Tim Van Baak</string>
</resources> </resources>