| /* |
| * Copyright (C) 2009 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.protips; |
| |
| import android.app.PendingIntent; |
| import android.app.Service; |
| import android.app.AlarmManager; |
| import android.appwidget.AppWidgetManager; |
| import android.appwidget.AppWidgetProvider; |
| import android.content.BroadcastReceiver; |
| import android.content.ComponentName; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.net.Uri; |
| import android.os.IBinder; |
| import android.os.Bundle; |
| import android.os.Handler; |
| import android.os.HandlerThread; |
| import android.os.Looper; |
| import android.os.Message; |
| import android.os.SystemClock; |
| import android.text.format.Time; |
| import android.util.Log; |
| import android.widget.RemoteViews; |
| import android.view.View; |
| |
| import java.util.Random; |
| |
| /** Mister Widget appears on your home screen to provide helpful tips. */ |
| public class ProtipWidget extends AppWidgetProvider { |
| public static final String ACTION_NEXT_TIP = "com.android.misterwidget.NEXT_TIP"; |
| public static final String ACTION_POKE = "com.android.misterwidget.HEE_HEE"; |
| |
| public static final String EXTRA_TIP_NUMBER = "tip"; |
| public static final String EXTRA_TIMES = "times"; |
| |
| private static Random sRNG = new Random(); |
| |
| // initial appearance: eyes closed, no bubble |
| private int mIconRes = R.drawable.droidman_open; |
| private int mMessage = 0; |
| |
| private AppWidgetManager mWidgetManager = null; |
| private int[] mWidgetIds; |
| private Context mContext; |
| |
| private CharSequence[] mTips; |
| private CharSequence[] mTitles; |
| |
| private void setup(Context context) { |
| mContext = context; |
| mWidgetManager = AppWidgetManager.getInstance(context); |
| mWidgetIds = mWidgetManager.getAppWidgetIds(new ComponentName(context, ProtipWidget.class)); |
| mTips = mContext.getResources().getTextArray(R.array.tips); |
| mTitles = mContext.getResources().getTextArray(R.array.titles); |
| } |
| |
| public void goodmorning() { |
| mMessage = -1; |
| try { |
| setIcon(R.drawable.droidman_down_closed); |
| Thread.sleep(500); |
| setIcon(R.drawable.droidman_down_open); |
| Thread.sleep(200); |
| setIcon(R.drawable.droidman_down_closed); |
| Thread.sleep(100); |
| setIcon(R.drawable.droidman_down_open); |
| Thread.sleep(600); |
| } catch (InterruptedException ex) { |
| } |
| mMessage = 0; |
| mIconRes = R.drawable.droidman_open; |
| refresh(); |
| } |
| |
| @Override |
| public void onReceive(Context context, Intent intent) { |
| setup(context); |
| |
| // carry state along in every message |
| mMessage = intent.getIntExtra(EXTRA_TIP_NUMBER, 0); |
| |
| if (intent.getAction().equals(ACTION_NEXT_TIP)) { |
| refresh(); |
| } else if (intent.getAction().equals(ACTION_POKE)) { |
| blink(intent.getIntExtra(EXTRA_TIMES, 1)); |
| } else if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_ENABLED)) { |
| goodmorning(); |
| } else { |
| mIconRes = R.drawable.droidman_open; |
| refresh(); |
| } |
| } |
| |
| private void refresh() { |
| RemoteViews rv = buildUpdate(mContext); |
| for (int i : mWidgetIds) { |
| mWidgetManager.updateAppWidget(i, rv); |
| } |
| } |
| |
| private void setIcon(int resId) { |
| mIconRes = resId; |
| refresh(); |
| } |
| |
| private int getNextMessageIndex() { |
| return (mMessage + 1) % mTips.length; |
| } |
| |
| private void blink(int blinks) { |
| setIcon(R.drawable.droidman_closed); |
| try { |
| Thread.sleep(100); |
| while (0<--blinks) { |
| setIcon(R.drawable.droidman_open); |
| Thread.sleep(200); |
| setIcon(R.drawable.droidman_closed); |
| Thread.sleep(100); |
| } |
| } catch (InterruptedException ex) { } |
| setIcon(R.drawable.droidman_open); |
| } |
| |
| public RemoteViews buildUpdate(Context context) { |
| // Pick out month names from resources |
| RemoteViews updateViews = new RemoteViews( |
| context.getPackageName(), R.layout.widget); |
| |
| Intent bcast = new Intent(context, ProtipWidget.class); |
| bcast.setAction(ACTION_NEXT_TIP); |
| bcast.putExtra(EXTRA_TIP_NUMBER, getNextMessageIndex()); |
| PendingIntent pending = PendingIntent.getBroadcast( |
| context, 0, bcast, PendingIntent.FLAG_UPDATE_CURRENT); |
| updateViews.setOnClickPendingIntent(R.id.tip_bubble, pending); |
| |
| bcast = new Intent(context, ProtipWidget.class); |
| bcast.setAction(ACTION_POKE); |
| bcast.putExtra(EXTRA_TIP_NUMBER, mMessage); |
| bcast.putExtra(EXTRA_TIMES, 1); |
| pending = PendingIntent.getBroadcast( |
| context, 0, bcast, PendingIntent.FLAG_UPDATE_CURRENT); |
| updateViews.setOnClickPendingIntent(R.id.bugdroid, pending); |
| |
| if (mMessage >= 0) { |
| updateViews.setTextViewText(R.id.tip_message, mTips[mMessage]); |
| updateViews.setTextViewText(R.id.tip_header, mTitles[mMessage]); |
| updateViews.setTextViewText(R.id.tip_footer, |
| String.format(context.getResources().getString(R.string.pager_footer), |
| (1+mMessage), mTips.length)); |
| updateViews.setViewVisibility(R.id.tip_bubble, View.VISIBLE); |
| } else { |
| updateViews.setViewVisibility(R.id.tip_bubble, View.INVISIBLE); |
| } |
| |
| updateViews.setImageViewResource(R.id.bugdroid, mIconRes); |
| |
| return updateViews; |
| } |
| } |