11daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler/*
273c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler * Copyright (C) 2010 The Android Open Source Project
31daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler *
41daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler * Licensed under the Apache License, Version 2.0 (the "License");
51daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler * you may not use this file except in compliance with the License.
61daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler * You may obtain a copy of the License at
71daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler *
81daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler *      http://www.apache.org/licenses/LICENSE-2.0
91daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler *
101daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler * Unless required by applicable law or agreed to in writing, software
111daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler * distributed under the License is distributed on an "AS IS" BASIS,
121daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler * See the License for the specific language governing permissions and
141daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler * limitations under the License.
151daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler */
161daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
171daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerpackage com.android.protips;
181daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
191daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.app.PendingIntent;
201daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.appwidget.AppWidgetManager;
211daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.appwidget.AppWidgetProvider;
221daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.content.ComponentName;
231daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.content.Context;
2497994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandlerimport android.content.SharedPreferences;
251daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.content.Intent;
2673c7b9654f9b557015a41192af094325f507a0ebDaniel Sandlerimport android.content.res.Resources;
271daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.os.Handler;
281daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.os.HandlerThread;
291daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.util.Log;
301daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerimport android.view.View;
3173c7b9654f9b557015a41192af094325f507a0ebDaniel Sandlerimport android.widget.RemoteViews;
321daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
3373c7b9654f9b557015a41192af094325f507a0ebDaniel Sandlerimport java.util.regex.Matcher;
3473c7b9654f9b557015a41192af094325f507a0ebDaniel Sandlerimport java.util.regex.Pattern;
351daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
361daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler/** Mister Widget appears on your home screen to provide helpful tips. */
371daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandlerpublic class ProtipWidget extends AppWidgetProvider {
381daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    public static final String ACTION_NEXT_TIP = "com.android.misterwidget.NEXT_TIP";
391daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    public static final String ACTION_POKE = "com.android.misterwidget.HEE_HEE";
401daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
411daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    public static final String EXTRA_TIMES = "times";
421daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
4397994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler    public static final String PREFS_NAME = "Protips";
4497994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler    public static final String PREFS_TIP_NUMBER = "widget_tip";
45f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler    public static final String PREFS_TIP_SET = "widget_tip_set";
4697994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler
4797994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler    private static final Pattern sNewlineRegex = Pattern.compile(" *\\n *");
4897994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler    private static final Pattern sDrawableRegex = Pattern.compile(" *@(drawable/[a-z0-9_]+) *");
4997994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler
50f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn    private static Handler mAsyncHandler;
51f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn    static {
52f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn        HandlerThread thr = new HandlerThread("ProtipWidget async");
53f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn        thr.start();
54f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn        mAsyncHandler = new Handler(thr.getLooper());
55f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn    }
56f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn
571daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    // initial appearance: eyes closed, no bubble
581daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private int mIconRes = R.drawable.droidman_open;
591daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private int mMessage = 0;
60f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler    private int mTipSet = 0;
611daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
621daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private AppWidgetManager mWidgetManager = null;
631daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private int[] mWidgetIds;
641daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private Context mContext;
651daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
661daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private CharSequence[] mTips;
671daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
681daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private void setup(Context context) {
691daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        mContext = context;
701daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        mWidgetManager = AppWidgetManager.getInstance(context);
711daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        mWidgetIds = mWidgetManager.getAppWidgetIds(new ComponentName(context, ProtipWidget.class));
7297994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler
7397994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler        SharedPreferences pref = context.getSharedPreferences(PREFS_NAME, 0);
7497994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler        mMessage = pref.getInt(PREFS_TIP_NUMBER, 0);
75f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler        mTipSet = pref.getInt(PREFS_TIP_SET, 0);
7697994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler
77f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler        mTips = context.getResources().getTextArray(mTipSet == 1 ? R.array.tips2 : R.array.tips);
7897994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler
7997994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler        if (mTips != null) {
8097994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            if (mMessage >= mTips.length) mMessage = 0;
8197994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler        } else {
8297994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            mMessage = -1;
8397994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler        }
841daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    }
851daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
861daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    public void goodmorning() {
871daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        mMessage = -1;
881daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        try {
891daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            setIcon(R.drawable.droidman_down_closed);
901daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            Thread.sleep(500);
911daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            setIcon(R.drawable.droidman_down_open);
921daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            Thread.sleep(200);
931daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            setIcon(R.drawable.droidman_down_closed);
941daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            Thread.sleep(100);
951daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            setIcon(R.drawable.droidman_down_open);
961daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            Thread.sleep(600);
971daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        } catch (InterruptedException ex) {
981daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        }
991daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        mMessage = 0;
1001daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        mIconRes = R.drawable.droidman_open;
1011daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        refresh();
1021daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    }
1031daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
1041daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    @Override
105f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn    public void onReceive(final Context context, final Intent intent) {
106f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn        final PendingResult result = goAsync();
107f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn        Runnable worker = new Runnable() {
108f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn            @Override
109f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn            public void run() {
110f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn                onReceiveAsync(context, intent);
111f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn                result.finish();
112f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn            }
113f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn        };
114f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn        mAsyncHandler.post(worker);
115f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn    }
116f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn
117f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn    void onReceiveAsync(Context context, Intent intent) {
1181daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        setup(context);
1191daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
120f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler        Resources res = mContext.getResources();
121f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler        mTips = res.getTextArray(mTipSet == 1 ? R.array.tips2 : R.array.tips);
122f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler
1231daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        if (intent.getAction().equals(ACTION_NEXT_TIP)) {
12497994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            mMessage = getNextMessageIndex();
12597994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            SharedPreferences.Editor pref = context.getSharedPreferences(PREFS_NAME, 0).edit();
12697994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            pref.putInt(PREFS_TIP_NUMBER, mMessage);
127f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn            pref.apply();
1281daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            refresh();
1291daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        } else if (intent.getAction().equals(ACTION_POKE)) {
1301daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            blink(intent.getIntExtra(EXTRA_TIMES, 1));
1311daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        } else if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_ENABLED)) {
1321daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            goodmorning();
133f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler        } else if (intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
134f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            Log.d("Protips", "ACHIEVEMENT UNLOCKED");
135f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            mTipSet = 1 - mTipSet;
136f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            mMessage = 0;
137f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler
138f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            SharedPreferences.Editor pref = context.getSharedPreferences(PREFS_NAME, 0).edit();
139f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            pref.putInt(PREFS_TIP_NUMBER, mMessage);
140f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            pref.putInt(PREFS_TIP_SET, mTipSet);
141f041447b816414de8ac6607263ff356557e007b3Dianne Hackborn            pref.apply();
142f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler
143f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            mContext.startActivity(
144f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler                new Intent(Intent.ACTION_MAIN)
145f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP)
146f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler                    .addCategory(Intent.CATEGORY_HOME));
147f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler
148f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            final Intent bcast = new Intent(context, ProtipWidget.class);
149f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            bcast.setAction(ACTION_POKE);
150f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            bcast.putExtra(EXTRA_TIMES, 3);
151f82202d691f7988584252d87e670de7a6e125fe5Daniel Sandler            mContext.sendBroadcast(bcast);
1521daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        } else {
1531daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            mIconRes = R.drawable.droidman_open;
1541daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            refresh();
1551daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        }
1561daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    }
1571daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
1581daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private void refresh() {
1591daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        RemoteViews rv = buildUpdate(mContext);
1601daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        for (int i : mWidgetIds) {
1611daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            mWidgetManager.updateAppWidget(i, rv);
1621daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        }
1631daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    }
1641daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
1651daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private void setIcon(int resId) {
1661daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        mIconRes = resId;
1671daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        refresh();
1681daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    }
1691daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
1701daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private int getNextMessageIndex() {
1711daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        return (mMessage + 1) % mTips.length;
1721daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    }
1731daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
1741daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    private void blink(int blinks) {
1758a92fcc8395c9d922abc2e772ea28b7920e26521Daniel Sandler        // don't blink if no bubble showing or if goodmorning() is happening
1768a92fcc8395c9d922abc2e772ea28b7920e26521Daniel Sandler        if (mMessage < 0) return;
1778a92fcc8395c9d922abc2e772ea28b7920e26521Daniel Sandler
1781daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        setIcon(R.drawable.droidman_closed);
1791daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        try {
1801daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            Thread.sleep(100);
1811daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            while (0<--blinks) {
1821daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler                setIcon(R.drawable.droidman_open);
1831daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler                Thread.sleep(200);
1841daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler                setIcon(R.drawable.droidman_closed);
1851daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler                Thread.sleep(100);
1861daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            }
1871daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        } catch (InterruptedException ex) { }
1881daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        setIcon(R.drawable.droidman_open);
1891daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    }
1901daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
1911daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    public RemoteViews buildUpdate(Context context) {
1921daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        RemoteViews updateViews = new RemoteViews(
1931daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            context.getPackageName(), R.layout.widget);
1941daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
19597994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler        // Action for tap on bubble
1961daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        Intent bcast = new Intent(context, ProtipWidget.class);
1971daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        bcast.setAction(ACTION_NEXT_TIP);
1981daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        PendingIntent pending = PendingIntent.getBroadcast(
1991daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            context, 0, bcast, PendingIntent.FLAG_UPDATE_CURRENT);
2001daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        updateViews.setOnClickPendingIntent(R.id.tip_bubble, pending);
2011daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
20297994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler        // Action for tap on android
2031daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        bcast = new Intent(context, ProtipWidget.class);
2041daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        bcast.setAction(ACTION_POKE);
2051daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        bcast.putExtra(EXTRA_TIMES, 1);
2061daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        pending = PendingIntent.getBroadcast(
2071daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            context, 0, bcast, PendingIntent.FLAG_UPDATE_CURRENT);
2081daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        updateViews.setOnClickPendingIntent(R.id.bugdroid, pending);
2091daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
21097994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler        // Tip bubble text
2111daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        if (mMessage >= 0) {
21297994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            String[] parts = sNewlineRegex.split(mTips[mMessage], 2);
21397994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            String title = parts[0];
21497994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            String text = parts.length > 1 ? parts[1] : "";
21597994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler
21697994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            // Look for a callout graphic referenced in the text
21797994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler            Matcher m = sDrawableRegex.matcher(text);
21873c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler            if (m.find()) {
21973c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                String imageName = m.group(1);
22073c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                int resId = context.getResources().getIdentifier(
22197994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler
22273c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                    imageName, null, context.getPackageName());
22373c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                updateViews.setImageViewResource(R.id.tip_callout, resId);
22473c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                updateViews.setViewVisibility(R.id.tip_callout, View.VISIBLE);
22573c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                text = m.replaceFirst("");
22673c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler            } else {
22773c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                updateViews.setImageViewResource(R.id.tip_callout, 0);
22873c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                updateViews.setViewVisibility(R.id.tip_callout, View.GONE);
22973c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler            }
23073c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler
23173c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler            updateViews.setTextViewText(R.id.tip_message,
23273c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler                text);
23373c7b9654f9b557015a41192af094325f507a0ebDaniel Sandler            updateViews.setTextViewText(R.id.tip_header,
23497994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler                title);
2351daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            updateViews.setTextViewText(R.id.tip_footer,
23697994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler                context.getResources().getString(
23797994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler                    R.string.pager_footer,
23897994f1b9bc9cb6cbf2fa303ea21f39351de1342Daniel Sandler                    (1+mMessage), mTips.length));
2391daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            updateViews.setViewVisibility(R.id.tip_bubble, View.VISIBLE);
2401daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        } else {
2411daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler            updateViews.setViewVisibility(R.id.tip_bubble, View.INVISIBLE);
2421daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        }
2431daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
2441daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        updateViews.setImageViewResource(R.id.bugdroid, mIconRes);
2451daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler
2461daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler        return updateViews;
2471daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler    }
2481daa55f4f4bfc13dcb8bcf66d410f8a79ad05f5aDaniel Sandler}
249