Support pre-HC style account shortcuts

Account shortcuts used to point at MessageList directly with a
content://com.android.email.provider/account/ACCOUNT-UUID URI.

Hook these intents and open Welcome instead.

On Eclair and before, we stored an account-ID directly as an extra,
but this style is no longer supported.

Bug 4208879

Change-Id: I9fecb0723743377a6d7c7e84626e8613f2356492
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 06f5f20..6aaa8e5 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -256,6 +256,14 @@
             </intent-filter>
         </activity>
 
+        <activity
+            android:name=".activity.MessageList"
+            >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+            </intent-filter>
+        </activity>
+
         <receiver
             android:name=".service.AttachmentDownloadService$Watchdog"
             android:enabled="true"/>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 3f7e549..0e594af 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -943,6 +943,11 @@
     <!-- Title of activity/dialog containing shortcut picker (list of accounts) [CHAR_LIMIT=20] -->
     <string name="account_shortcut_picker_title">Select an account</string>
 
+    <!-- Toast shown when the selected account no longer exists.  This is used when, for example,
+         a shotcut or a widget is stale and points at a deleted account.
+         [CHAR_LIMIT=none]-->
+    <string name="toast_account_not_found">Account not found. It may have been removed.</string>
+
     <!-- Message that appears when adding a Windows Live Hotmail Plus account -->
     <string name="provider_note_live">Only some \"Plus\" accounts include POP access
         allowing this program to connect. If you are not able to sign in with
diff --git a/src/com/android/email/activity/AccountShortcutPicker.java b/src/com/android/email/activity/AccountShortcutPicker.java
index 475a6d0..2bfd8b8 100644
--- a/src/com/android/email/activity/AccountShortcutPicker.java
+++ b/src/com/android/email/activity/AccountShortcutPicker.java
@@ -17,6 +17,7 @@
 package com.android.email.activity;
 
 import com.android.email.R;
+import com.android.emailcommon.Logging;
 import com.android.emailcommon.provider.EmailContent.Account;
 import com.android.emailcommon.provider.EmailContent.AccountColumns;
 
@@ -28,6 +29,7 @@
 import android.database.Cursor;
 import android.os.Bundle;
 import android.os.Parcelable;
+import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.AdapterView;
@@ -37,13 +39,19 @@
 
 /**
  * This class implements a launcher shortcut for directly accessing a single account.
- *
- * TODO Handle upgraded shortcuts for the phone UI release.  Shortcuts used to launch MessageList
- * directly.  We need to detect this and redirect to Welcome.
  */
 public class AccountShortcutPicker extends ListActivity
         implements OnClickListener, OnItemClickListener, LoaderCallbacks<Cursor> {
 
+    /**
+     * Debug flag -- if true, create pre-honeycomb style shortcuts.
+     *
+     * This allows developers to test launching the app from old style shortcuts (which point at
+     * MessageList rather than Welcome) without actually carrying over shortcuts from previous
+     * versions.
+     */
+    private static final boolean TEST_CREATE_OLD_STYLE_SHORTCUT = false; // DO NOT SUBMIT WITH TRUE
+
     @SuppressWarnings("hiding")
     private SimpleCursorAdapter mAdapter;
 
@@ -132,7 +140,13 @@
      */
     private void setupShortcut(Account account) {
         // First, set up the shortcut intent.
-        Intent shortcutIntent = Welcome.createAccountShortcutIntent(this, account);
+        final Intent shortcutIntent;
+        if (TEST_CREATE_OLD_STYLE_SHORTCUT) {
+            shortcutIntent = MessageList.createFroyoIntent(this, account);
+            Log.d(Logging.LOG_TAG, "Created old style intent: " + shortcutIntent);
+        } else {
+            shortcutIntent = Welcome.createAccountShortcutIntent(this, account);
+        }
 
         // Then, set up the container intent (the response to the caller)
         Intent intent = new Intent();
@@ -140,6 +154,7 @@
         intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, account.getDisplayName());
         Parcelable iconResource
                 = Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher_email);
+
         intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
 
         // Now, return the result to the launcher
diff --git a/src/com/android/email/activity/MessageList.java b/src/com/android/email/activity/MessageList.java
new file mode 100644
index 0000000..5fc9602
--- /dev/null
+++ b/src/com/android/email/activity/MessageList.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2011 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.email.activity;
+
+import com.android.email.R;
+import com.android.emailcommon.provider.EmailContent.Account;
+import com.android.emailcommon.utility.EmailAsyncTask;
+import com.android.emailcommon.utility.Utility;
+import com.google.common.annotations.VisibleForTesting;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Bundle;
+
+/**
+ * A dummy activity to support old-style (pre-honeycomb) account shortcuts.
+ */
+public class MessageList extends Activity {
+    @VisibleForTesting
+    static final String EXTRA_ACCOUNT_ID = "com.android.email.activity._ACCOUNT_ID";
+
+    private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker();
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final Activity me = this;
+        new EmailAsyncTask<Void, Void, Long>(mTaskTracker) {
+            @Override
+            protected Long doInBackground(Void... params) {
+                return getAccountFromIntent(me, getIntent());
+            }
+
+            @Override
+            protected void onPostExecute(Long accountId) {
+                if ((accountId == null) || (accountId == Account.NO_ACCOUNT)) {
+                    // Account deleted?
+                    Utility.showToast(me, R.string.toast_account_not_found);
+                    Welcome.actionStart(me);
+                } else {
+                    Welcome.actionOpenAccountInbox(me, accountId);
+                }
+                finish();
+            }
+        }.executeParallel();
+    }
+
+    @Override
+    protected void onDestroy() {
+        mTaskTracker.cancellAllInterrupt();
+        super.onDestroy();
+    }
+
+    @VisibleForTesting
+    static long getAccountFromIntent(Context context, Intent i) {
+        final Uri uri = i.getData();
+        if (uri == null) {
+            return Account.NO_ACCOUNT;
+        }
+        return Account.getAccountIdFromShortcutSafeUri(context, uri);
+    }
+
+    /**
+     * Create a froyo/gingerbread style account shortcut intent.  Used by unit tests and
+     * test code in {@link AccountShortcutPicker}.
+     */
+    @VisibleForTesting
+    static Intent createFroyoIntent(Context context, Account account) {
+        final Intent intent = new Intent(context, MessageList.class);
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+        intent.putExtra(EXTRA_ACCOUNT_ID, account.mId);
+        intent.setData(account.getShortcutSafeUri());
+
+        return intent;
+    }
+}
diff --git a/src/com/android/email/activity/Welcome.java b/src/com/android/email/activity/Welcome.java
index 4d2ee4d..0edd14e 100644
--- a/src/com/android/email/activity/Welcome.java
+++ b/src/com/android/email/activity/Welcome.java
@@ -211,6 +211,8 @@
 
         @VisibleForTesting
         static long resolveAccountId(Context context, long accountId, String uuid) {
+            // TODO show "account may have been removed" toast when an account is specified but
+            // can't find it.
             if (!TextUtils.isEmpty(uuid)) {
                 accountId = Account.getAccountIdFromUuid(context, uuid);
             }
diff --git a/tests/src/com/android/email/activity/MessageListTests.java b/tests/src/com/android/email/activity/MessageListTests.java
new file mode 100644
index 0000000..d282806
--- /dev/null
+++ b/tests/src/com/android/email/activity/MessageListTests.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2011 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.email.activity;
+
+import com.android.email.DBTestHelper;
+import com.android.email.provider.ProviderTestUtils;
+import com.android.emailcommon.provider.EmailContent.Account;
+
+import android.content.Context;
+import android.content.Intent;
+import android.test.AndroidTestCase;
+
+public class MessageListTests extends AndroidTestCase  {
+
+    private Context mMockContext;
+
+    public MessageListTests() {
+    }
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+
+        // ProviderTestCase2 can't be used.  It creates a mock context that doesn't support
+        // some methods we need here, such as getPackageName.
+        mMockContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(
+                getContext());
+    }
+
+    public void testGetAccountFromIntent() {
+        final Context c = mMockContext;
+        final Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
+        final Account a2 = ProviderTestUtils.setupAccount("a2", true, c);
+
+        assertEquals(a1.mId, MessageList.getAccountFromIntent(c,
+                MessageList.createFroyoIntent(c, a1)));
+        assertEquals(a2.mId, MessageList.getAccountFromIntent(c,
+                MessageList.createFroyoIntent(c, a2)));
+
+        // Mixed -- UUID in the URI doesn't match the account ID in extra.
+        // It's a test for shortcuts for restored accounts.
+        final Intent i = MessageList.createFroyoIntent(c, a2);
+        i.putExtra(MessageList.EXTRA_ACCOUNT_ID, 12345);
+        assertEquals(a2.mId, MessageList.getAccountFromIntent(c, i));
+
+        // Invalid intent -- no extra, no URI.
+        assertEquals(Account.NO_ACCOUNT, MessageList.getAccountFromIntent(c,
+                new Intent(c, MessageList.class)));
+    }
+}