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 {
27308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    // Format for activity URIs: content://ui.email.android.com/...
28308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private static final String ACTIVITY_INTENT_SCHEME = "content";
29308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private static final String ACTIVITY_INTENT_HOST = "ui.email.android.com";
30308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
31308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private static final String ACCOUNT_ID_PARAM = "ACCOUNT_ID";
32308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private static final String MAILBOX_ID_PARAM = "MAILBOX_ID";
33308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private static final String MESSAGE_ID_PARAM = "MESSAGE_ID";
340b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki    private static final String ACCOUNT_UUID_PARAM = "ACCOUNT_UUID";
35308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
36308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private IntentUtilities() {
37308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
38308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
39308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
40308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * @return a URI builder for "content://ui.email.android.com/..."
41308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
42308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static Uri.Builder createActivityIntentUrlBuilder(String path) {
43308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        final Uri.Builder b = new Uri.Builder();
44308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        b.scheme(ACTIVITY_INTENT_SCHEME);
45308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        b.authority(ACTIVITY_INTENT_HOST);
46308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        b.path(path);
47308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        return b;
48308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
49308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
50308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
51308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * Add the account ID parameter.
52308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
53308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static void setAccountId(Uri.Builder b, long accountId) {
54308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        if (accountId != -1) {
55308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki            b.appendQueryParameter(ACCOUNT_ID_PARAM, Long.toString(accountId));
56308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        }
57308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
58308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
59308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
60308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * Add the mailbox ID parameter.
61308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
62308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static void setMailboxId(Uri.Builder b, long mailboxId) {
63308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        if (mailboxId != -1) {
64308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki            b.appendQueryParameter(MAILBOX_ID_PARAM, Long.toString(mailboxId));
65308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        }
66308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
67308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
68308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
69308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * Add the message ID parameter.
70308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
71308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static void setMessageId(Uri.Builder b, long messageId) {
72308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        if (messageId != -1) {
73308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki            b.appendQueryParameter(MESSAGE_ID_PARAM, Long.toString(messageId));
74308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        }
75308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
76308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
77308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
780b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki     * Add the account UUID parameter.
790b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki     */
800b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki    public static void setAccountUuid(Uri.Builder b, String mUuid) {
810b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki        if (TextUtils.isEmpty(mUuid)) {
820b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki            throw new IllegalArgumentException();
830b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki        }
840b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki        b.appendQueryParameter(ACCOUNT_UUID_PARAM, mUuid);
850b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki    }
860b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki
870b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki    /**
88308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * Retrieve the account ID.
89308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
90308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static long getAccountIdFromIntent(Intent intent) {
91308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        return getLongFromIntent(intent, ACCOUNT_ID_PARAM);
92308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
93308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
94308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
95308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * Retrieve the mailbox ID.
96308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
97308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static long getMailboxIdFromIntent(Intent intent) {
98308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        return getLongFromIntent(intent, MAILBOX_ID_PARAM);
99308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
100308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
101308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
102308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * Retrieve the message ID.
103308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
104308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static long getMessageIdFromIntent(Intent intent) {
105308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        return getLongFromIntent(intent, MESSAGE_ID_PARAM);
106308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
107308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
1080b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki    /**
1090b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki     * Retrieve the account UUID, or null if the UUID param is not found.
1100b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki     */
1110b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki    public static String getAccountUuidFromIntent(Intent intent) {
1120b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki        final Uri uri = intent.getData();
1130b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki        if (uri == null) {
1140b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki            return null;
1150b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki        }
1160b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki        String uuid = uri.getQueryParameter(ACCOUNT_UUID_PARAM);
1170b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki        return TextUtils.isEmpty(uuid) ? null : uuid;
1180b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki    }
1190b6cb1c3307b10929fca7dc09125a128fc330a19Makoto Onuki
120308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private static long getLongFromIntent(Intent intent, String paramName) {
121308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        long value = -1;
122308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        if (intent.getData() != null) {
123308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki            value = getLongParamFromUri(intent.getData(), paramName, -1);
124308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        }
125308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        return value;
126308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
127308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
128308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private static long getLongParamFromUri(Uri uri, String paramName, long defaultValue) {
129308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        final String value = uri.getQueryParameter(paramName);
130308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        if (!TextUtils.isEmpty(value)) {
131308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki            try {
132308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki                return Long.parseLong(value);
133308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki            } catch (NumberFormatException e) {
134308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki                // return default
135308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki            }
136308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        }
137308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        return defaultValue;
138308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
139308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
140308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
141308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * Create an {@link Intent} to launch an activity as the main entry point.  Existing activities
142308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * will all be closed.
143308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
144308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static Intent createRestartAppIntent(Context context, Class<? extends Activity> clazz) {
145308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        Intent i = new Intent(context, clazz);
146308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        prepareRestartAppIntent(i);
147308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        return i;
148308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
149308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
150308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    /**
151308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * Create an {@link Intent} to launch an activity as the main entry point.  Existing activities
152308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     * will all be closed.
153308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki     */
154308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    public static Intent createRestartAppIntent(Uri data) {
155308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        Intent i = new Intent(Intent.ACTION_MAIN, data);
156308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        prepareRestartAppIntent(i);
157308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        return i;
158308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
159308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki
160308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    private static void prepareRestartAppIntent(Intent i) {
161308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        i.setAction(Intent.ACTION_MAIN);
162308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        i.addCategory(Intent.CATEGORY_LAUNCHER);
163308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
164308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
165308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki    }
166308ce9284793b597797994dfb1fb25155cbe0b20Makoto Onuki}
167