1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.emailcommon.utility;
18
19
20import android.app.Activity;
21import android.content.Context;
22import android.content.Intent;
23import android.net.Uri;
24import android.text.TextUtils;
25
26public final class IntentUtilities {
27
28    public static final String PATH_SETTINGS = "settings";
29
30    // Format for activity URIs: content://ui.email.android.com/...
31    private static final String ACTIVITY_INTENT_SCHEME = "content";
32    private static final String ACTIVITY_INTENT_HOST = "ui.email.android.com";
33
34    private static final String ACCOUNT_ID_PARAM = "ACCOUNT_ID";
35    private static final String ACCOUNT_NAME_PARAM = "ACCOUNT_NAME";
36    private static final String MAILBOX_ID_PARAM = "MAILBOX_ID";
37    private static final String MESSAGE_ID_PARAM = "MESSAGE_ID";
38    private static final String ACCOUNT_UUID_PARAM = "ACCOUNT_UUID";
39
40    private IntentUtilities() {
41    }
42
43    /**
44     * @return a URI builder for "content://ui.email.android.com/..."
45     */
46    public static Uri.Builder createActivityIntentUrlBuilder(String path) {
47        final Uri.Builder b = new Uri.Builder();
48        b.scheme(ACTIVITY_INTENT_SCHEME);
49        b.authority(ACTIVITY_INTENT_HOST);
50        b.path(path);
51        return b;
52    }
53
54    /**
55     * Add the account ID parameter.
56     */
57    public static void setAccountId(Uri.Builder b, long accountId) {
58        if (accountId != -1) {
59            b.appendQueryParameter(ACCOUNT_ID_PARAM, Long.toString(accountId));
60        }
61    }
62
63    /**
64     * Add the account name parameter.
65     */
66    public static void setAccountName(Uri.Builder b, String accountName) {
67        if (accountName != null) {
68            b.appendQueryParameter(ACCOUNT_NAME_PARAM, accountName);
69        }
70    }
71
72    /**
73     * Add the mailbox ID parameter.
74     */
75    public static void setMailboxId(Uri.Builder b, long mailboxId) {
76        if (mailboxId != -1) {
77            b.appendQueryParameter(MAILBOX_ID_PARAM, Long.toString(mailboxId));
78        }
79    }
80
81    /**
82     * Add the message ID parameter.
83     */
84    public static void setMessageId(Uri.Builder b, long messageId) {
85        if (messageId != -1) {
86            b.appendQueryParameter(MESSAGE_ID_PARAM, Long.toString(messageId));
87        }
88    }
89
90    /**
91     * Add the account UUID parameter.
92     */
93    public static void setAccountUuid(Uri.Builder b, String mUuid) {
94        if (TextUtils.isEmpty(mUuid)) {
95            throw new IllegalArgumentException();
96        }
97        b.appendQueryParameter(ACCOUNT_UUID_PARAM, mUuid);
98    }
99
100    /**
101     * Retrieve the account ID from the underlying URI.
102     */
103    public static long getAccountIdFromIntent(Intent intent) {
104        return getLongFromIntent(intent, ACCOUNT_ID_PARAM);
105    }
106
107    /**
108     * Retrieve the account name.
109     */
110    public static String getAccountNameFromIntent(Intent intent) {
111        return getStringFromIntent(intent, ACCOUNT_NAME_PARAM);
112    }
113
114    /**
115     * Retrieve the mailbox ID.
116     */
117    public static long getMailboxIdFromIntent(Intent intent) {
118        return getLongFromIntent(intent, MAILBOX_ID_PARAM);
119    }
120
121    /**
122     * Retrieve the message ID.
123     */
124    public static long getMessageIdFromIntent(Intent intent) {
125        return getLongFromIntent(intent, MESSAGE_ID_PARAM);
126    }
127
128    /**
129     * Retrieve the account UUID, or null if the UUID param is not found.
130     */
131    public static String getAccountUuidFromIntent(Intent intent) {
132        final Uri uri = intent.getData();
133        if (uri == null) {
134            return null;
135        }
136        String uuid = uri.getQueryParameter(ACCOUNT_UUID_PARAM);
137        return TextUtils.isEmpty(uuid) ? null : uuid;
138    }
139
140    private static long getLongFromIntent(Intent intent, String paramName) {
141        long value = -1;
142        if (intent.getData() != null) {
143            value = getLongParamFromUri(intent.getData(), paramName, -1);
144        }
145        return value;
146    }
147
148    private static String getStringFromIntent(Intent intent, String paramName) {
149        String value = null;
150        if (intent.getData() != null) {
151            value = getStringParamFromUri(intent.getData(), paramName, null);
152        }
153        return value;
154    }
155
156    private static long getLongParamFromUri(Uri uri, String paramName, long defaultValue) {
157        final String value = uri.getQueryParameter(paramName);
158        if (!TextUtils.isEmpty(value)) {
159            try {
160                return Long.parseLong(value);
161            } catch (NumberFormatException e) {
162                // return default
163            }
164        }
165        return defaultValue;
166    }
167
168    private static String getStringParamFromUri(Uri uri, String paramName, String defaultValue) {
169        final String value = uri.getQueryParameter(paramName);
170        if (value == null) {
171            return defaultValue;
172        }
173        return value;
174    }
175
176    /**
177     * Create an {@link Intent} to launch an activity as the main entry point.  Existing activities
178     * will all be closed.
179     */
180    public static Intent createRestartAppIntent(Context context, Class<? extends Activity> clazz) {
181        Intent i = new Intent(context, clazz);
182        prepareRestartAppIntent(i);
183        return i;
184    }
185
186    /**
187     * Create an {@link Intent} to launch an activity as the main entry point.  Existing activities
188     * will all be closed.
189     */
190    public static Intent createRestartAppIntent(Uri data) {
191        Intent i = new Intent(Intent.ACTION_MAIN, data);
192        prepareRestartAppIntent(i);
193        return i;
194    }
195
196    private static void prepareRestartAppIntent(Intent i) {
197        i.setAction(Intent.ACTION_MAIN);
198        i.addCategory(Intent.CATEGORY_LAUNCHER);
199        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
200        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
201    }
202}
203