Permalink
Browse files

[New Feature] Add share button on widget

Share button for easy sharing the picture on widget

Signed-off-by: Shaka Huang <shaka@cs03.org>
  • Loading branch information...
1 parent 3d5c2de commit 61dd467e1cc2fb0a92632c6319a4c36c01a42e5e @shakalaca committed Nov 30, 2011
View
1 README
@@ -10,6 +10,7 @@ E-mail: shakalaca AT gmail dot com
v2.5:
* Support 早稲田
+* Support widget with picture and share button
v2.4:
* New share picture layout
@@ -17,8 +17,6 @@
android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1"
- android:layout_marginLeft="6dip"
- android:layout_marginRight="6dip"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/TimeTextView"
@@ -32,4 +30,13 @@
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/beautyclock_widget_title" />
+ <ImageView
+ android:id="@+id/ShareIt"
+ android:layout_alignParentRight="true"
+ android:layout_alignParentBottom="true"
+ android:layout_height="24dp"
+ android:layout_width="24dp"
+ android:layout_marginLeft="6dp"
+ android:layout_marginBottom="6dp"
+ android:background="@drawable/share" />
</RelativeLayout>
@@ -13,15 +13,18 @@
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.net.Uri;
import android.text.format.Time;
import android.util.Log;
+import android.view.View;
import android.widget.RemoteViews;
public class WidgetProvider extends AppWidgetProvider {
private static final String DISPLAYTIME_FORMAT = "%02d:%02d";
private static final String TAG = "WidgetProvider";
private Time mTime = new Time();
+ private File PictureFile = null;
@Override
public void onEnabled(Context context) {
@@ -46,13 +49,15 @@ private Bitmap updateBeautyBitmap(Context context) {
// check SD card first
String fname = String.format("%s/%02d%02d.jpg", mStorePath, hour, minute);
File _f_sdcard = new File(fname);
+ PictureFile = _f_sdcard;
if (!_f_sdcard.exists()) {
fname = String.format("%s/%02d%02d.jpg", context.getCacheDir().getAbsolutePath(), hour, minute);
File _f_cache = new File(fname);
if (!_f_cache.exists()) {
return null;
}
+ PictureFile = _f_cache;
}
return BitmapFactory.decodeFile(fname);
@@ -88,9 +93,20 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
Bitmap bitmap = updateBeautyBitmap(context);
if (bitmap == null) {
views.setImageViewResource(R.id.BeautyClockImageView, R.drawable.beautyclock_retry);
+ views.setViewVisibility(R.id.ShareIt, View.GONE);
} else {
views.setImageViewBitmap(R.id.BeautyClockImageView, bitmap);
- }
+
+ Intent shareIntent = new Intent(Intent.ACTION_SEND);
+ shareIntent.setType("image/jpeg");
+ shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(PictureFile));
+ shareIntent.putExtra(Intent.EXTRA_SUBJECT, context.getResources().getString(R.string.share_picture_subject_text));
+ shareIntent.putExtra(Intent.EXTRA_TEXT, context.getResources().getString(R.string.share_picture_msg_text));
+ shareIntent.putExtra(Intent.EXTRA_TITLE, context.getResources().getString(R.string.share_picture_title_text));
+ PendingIntent pi_share = PendingIntent.getActivity(context, 0, shareIntent, 0);
+
+ views.setOnClickPendingIntent(R.id.ShareIt, pi_share);
+ }
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetIds, views);
@@ -18,9 +18,11 @@
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.net.Uri;
import android.os.IBinder;
import android.text.format.Time;
import android.util.Log;
+import android.view.View;
import android.widget.RemoteViews;
public class WidgetService extends Service implements SharedPreferences.OnSharedPreferenceChangeListener {
@@ -34,6 +36,7 @@
private int mScreenWidth = 0;
private SharedPreferences mPrefs;
+ private File PictureFile = null;
private boolean mRegScreenBR = false;
private boolean mRegTimeBR = false;
@@ -148,6 +151,7 @@ private Bitmap updateBeautyBitmap() {
// check SD card first
String fname = String.format("%s/%02d%02d.jpg", storePath, hour, minute);
File _f_sdcard = new File(fname);
+ PictureFile = _f_sdcard;
if (!_f_sdcard.exists()) {
fname = String.format("%s/%02d%02d.jpg", getCacheDir().getAbsolutePath(), hour, minute);
File _f_cache = new File(fname);
@@ -160,6 +164,7 @@ private Bitmap updateBeautyBitmap() {
return null;
}
+ PictureFile = _f_cache;
}
Log.d(TAG, fname);
@@ -231,13 +236,24 @@ private void updateWidget(Context context) {
Bitmap bitmap = updateBeautyBitmap();
if (bitmap == null) {
remoteViews.setImageViewResource(R.id.BeautyClockImageView, R.drawable.beautyclock_retry);
+ remoteViews.setViewVisibility(R.id.ShareIt, View.GONE);
} else {
// Bitmap bitmap_scaled = ResizeBitmap(bitmap);
// if (bitmap_scaled != null) {
// remoteViews.setImageViewBitmap(R.id.BeautyClockImageView, bitmap_scaled);
// } else {
remoteViews.setImageViewBitmap(R.id.BeautyClockImageView, bitmap);
// }
+
+ Intent shareIntent = new Intent(Intent.ACTION_SEND);
+ shareIntent.setType("image/jpeg");
+ shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(PictureFile));
+ shareIntent.putExtra(Intent.EXTRA_SUBJECT, context.getResources().getString(R.string.share_picture_subject_text));
+ shareIntent.putExtra(Intent.EXTRA_TEXT, context.getResources().getString(R.string.share_picture_msg_text));
+ shareIntent.putExtra(Intent.EXTRA_TITLE, context.getResources().getString(R.string.share_picture_title_text));
+ PendingIntent pi_share = PendingIntent.getActivity(context, 0, shareIntent, 0);
+
+ remoteViews.setOnClickPendingIntent(R.id.ShareIt, pi_share);
}
awm.updateAppWidget(remoteWidget, remoteViews);

0 comments on commit 61dd467

Please sign in to comment.