1308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki/* 2308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Copyright (C) 2011 The Android Open Source Project 3308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * 4308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Licensed under the Apache License, Version 2.0 (the "License"); 5308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * you may not use this file except in compliance with the License. 6308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * You may obtain a copy of the License at 7308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * 8308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * http://www.apache.org/licenses/LICENSE-2.0 9308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * 10308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Unless required by applicable law or agreed to in writing, software 11308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * distributed under the License is distributed on an "AS IS" BASIS, 12308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * See the License for the specific language governing permissions and 14308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * limitations under the License. 15308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 16308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 170e6a521747970d5427f10c25cdc070d2341dc93aBen Komalopackage com.android.emailcommon.utility; 18308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 190b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki 20308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onukiimport android.app.Activity; 21308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onukiimport android.content.Context; 22308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onukiimport android.content.Intent; 23308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onukiimport android.net.Uri; 24308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onukiimport android.text.TextUtils; 25308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 26308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onukipublic final class IntentUtilities { 27c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert 28c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert public static final String PATH_SETTINGS = "settings"; 29c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert 30308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki // Format for activity URIs: content://ui.email.android.com/... 31308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private static final String ACTIVITY_INTENT_SCHEME = "content"; 32308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private static final String ACTIVITY_INTENT_HOST = "ui.email.android.com"; 33308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 34308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private static final String ACCOUNT_ID_PARAM = "ACCOUNT_ID"; 35c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert private static final String ACCOUNT_NAME_PARAM = "ACCOUNT_NAME"; 36308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private static final String MAILBOX_ID_PARAM = "MAILBOX_ID"; 37308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private static final String MESSAGE_ID_PARAM = "MESSAGE_ID"; 380b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki private static final String ACCOUNT_UUID_PARAM = "ACCOUNT_UUID"; 39308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 40308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private IntentUtilities() { 41308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 42308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 43308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 44308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * @return a URI builder for "content://ui.email.android.com/..." 45308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 46308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static Uri.Builder createActivityIntentUrlBuilder(String path) { 47308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki final Uri.Builder b = new Uri.Builder(); 48308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki b.scheme(ACTIVITY_INTENT_SCHEME); 49308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki b.authority(ACTIVITY_INTENT_HOST); 50308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki b.path(path); 51308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return b; 52308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 53308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 54308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 55308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Add the account ID parameter. 56308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 57308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static void setAccountId(Uri.Builder b, long accountId) { 58308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki if (accountId != -1) { 59308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki b.appendQueryParameter(ACCOUNT_ID_PARAM, Long.toString(accountId)); 60308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 61308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 62308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 63308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 64c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert * Add the account name parameter. 65c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert */ 66c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert public static void setAccountName(Uri.Builder b, String accountName) { 67c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert if (accountName != null) { 68c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert b.appendQueryParameter(ACCOUNT_NAME_PARAM, accountName); 69c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert } 70c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert } 71c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert 72c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert /** 73308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Add the mailbox ID parameter. 74308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 75308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static void setMailboxId(Uri.Builder b, long mailboxId) { 76308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki if (mailboxId != -1) { 77308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki b.appendQueryParameter(MAILBOX_ID_PARAM, Long.toString(mailboxId)); 78308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 79308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 80308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 81308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 82308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Add the message ID parameter. 83308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 84308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static void setMessageId(Uri.Builder b, long messageId) { 85308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki if (messageId != -1) { 86308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki b.appendQueryParameter(MESSAGE_ID_PARAM, Long.toString(messageId)); 87308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 88308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 89308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 90308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 910b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki * Add the account UUID parameter. 920b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki */ 930b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki public static void setAccountUuid(Uri.Builder b, String mUuid) { 940b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki if (TextUtils.isEmpty(mUuid)) { 950b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki throw new IllegalArgumentException(); 960b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki } 970b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki b.appendQueryParameter(ACCOUNT_UUID_PARAM, mUuid); 980b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki } 990b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki 1000b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki /** 1010dffe3afd7a2fdfb394573aa0d8d06dd90e9fe12James Lemieux * Retrieve the account ID from the underlying URI. 102308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 103308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static long getAccountIdFromIntent(Intent intent) { 104308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return getLongFromIntent(intent, ACCOUNT_ID_PARAM); 105308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 106308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 107308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 108c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert * Retrieve the account name. 109c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert */ 110c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert public static String getAccountNameFromIntent(Intent intent) { 111c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert return getStringFromIntent(intent, ACCOUNT_NAME_PARAM); 112c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert } 113c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert 114c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert /** 115308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Retrieve the mailbox ID. 116308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 117308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static long getMailboxIdFromIntent(Intent intent) { 118308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return getLongFromIntent(intent, MAILBOX_ID_PARAM); 119308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 120308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 121308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 122308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Retrieve the message ID. 123308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 124308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static long getMessageIdFromIntent(Intent intent) { 125308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return getLongFromIntent(intent, MESSAGE_ID_PARAM); 126308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 127308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 1280b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki /** 1290b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki * Retrieve the account UUID, or null if the UUID param is not found. 1300b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki */ 1310b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki public static String getAccountUuidFromIntent(Intent intent) { 1320b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki final Uri uri = intent.getData(); 1330b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki if (uri == null) { 1340b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki return null; 1350b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki } 1360b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki String uuid = uri.getQueryParameter(ACCOUNT_UUID_PARAM); 1370b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki return TextUtils.isEmpty(uuid) ? null : uuid; 1380b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki } 1390b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki 140308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private static long getLongFromIntent(Intent intent, String paramName) { 141308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki long value = -1; 142308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki if (intent.getData() != null) { 143308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki value = getLongParamFromUri(intent.getData(), paramName, -1); 144308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 145308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return value; 146308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 147308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 148c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert private static String getStringFromIntent(Intent intent, String paramName) { 149c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert String value = null; 150c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert if (intent.getData() != null) { 151c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert value = getStringParamFromUri(intent.getData(), paramName, null); 152c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert } 153c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert return value; 154c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert } 155c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert 156308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private static long getLongParamFromUri(Uri uri, String paramName, long defaultValue) { 157308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki final String value = uri.getQueryParameter(paramName); 158308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki if (!TextUtils.isEmpty(value)) { 159308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki try { 160308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return Long.parseLong(value); 161308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } catch (NumberFormatException e) { 162308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki // return default 163308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 164308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 165308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return defaultValue; 166308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 167308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 168c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert private static String getStringParamFromUri(Uri uri, String paramName, String defaultValue) { 169c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert final String value = uri.getQueryParameter(paramName); 170c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert if (value == null) { 171c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert return defaultValue; 172c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert } 173c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert return value; 174c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert } 175c4d139c4f4d924eae0307e8349ae977441dabbedAlon Albert 176308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 177308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Create an {@link Intent} to launch an activity as the main entry point. Existing activities 178308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * will all be closed. 179308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 180308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static Intent createRestartAppIntent(Context context, Class<? extends Activity> clazz) { 181308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki Intent i = new Intent(context, clazz); 182308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki prepareRestartAppIntent(i); 183308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return i; 184308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 185308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 186308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki /** 187308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * Create an {@link Intent} to launch an activity as the main entry point. Existing activities 188308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki * will all be closed. 189308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki */ 190308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki public static Intent createRestartAppIntent(Uri data) { 191308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki Intent i = new Intent(Intent.ACTION_MAIN, data); 192308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki prepareRestartAppIntent(i); 193308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki return i; 194308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 195308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki 196308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki private static void prepareRestartAppIntent(Intent i) { 197308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki i.setAction(Intent.ACTION_MAIN); 198308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki i.addCategory(Intent.CATEGORY_LAUNCHER); 199308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 200308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 201308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki } 202308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki} 203