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    // Format for activity URIs: content://ui.email.android.com/...
28    private static final String ACTIVITY_INTENT_SCHEME = "content";
29    private static final String ACTIVITY_INTENT_HOST = "ui.email.android.com";
30
31    private static final String ACCOUNT_ID_PARAM = "ACCOUNT_ID";
32    private static final String MAILBOX_ID_PARAM = "MAILBOX_ID";
33    private static final String MESSAGE_ID_PARAM = "MESSAGE_ID";
34    private static final String ACCOUNT_UUID_PARAM = "ACCOUNT_UUID";
35
36    private IntentUtilities() {
37    }
38
39    /**
40     * @return a URI builder for "content://ui.email.android.com/..."
41     */
42    public static Uri.Builder createActivityIntentUrlBuilder(String path) {
43        final Uri.Builder b = new Uri.Builder();
44        b.scheme(ACTIVITY_INTENT_SCHEME);
45        b.authority(ACTIVITY_INTENT_HOST);
46        b.path(path);
47        return b;
48    }
49
50    /**
51     * Add the account ID parameter.
52     */
53    public static void setAccountId(Uri.Builder b, long accountId) {
54        if (accountId != -1) {
55            b.appendQueryParameter(ACCOUNT_ID_PARAM, Long.toString(accountId));
56        }
57    }
58
59    /**
60     * Add the mailbox ID parameter.
61     */
62    public static void setMailboxId(Uri.Builder b, long mailboxId) {
63        if (mailboxId != -1) {
64            b.appendQueryParameter(MAILBOX_ID_PARAM, Long.toString(mailboxId));
65        }
66    }
67
68    /**
69     * Add the message ID parameter.
70     */
71    public static void setMessageId(Uri.Builder b, long messageId) {
72        if (messageId != -1) {
73            b.appendQueryParameter(MESSAGE_ID_PARAM, Long.toString(messageId));
74        }
75    }
76
77    /**
78     * Add the account UUID parameter.
79     */
80    public static void setAccountUuid(Uri.Builder b, String mUuid) {
81        if (TextUtils.isEmpty(mUuid)) {
82            throw new IllegalArgumentException();
83        }
84        b.appendQueryParameter(ACCOUNT_UUID_PARAM, mUuid);
85    }
86
87    /**
88     * Retrieve the account ID.
89     */
90    public static long getAccountIdFromIntent(Intent intent) {
91        return getLongFromIntent(intent, ACCOUNT_ID_PARAM);
92    }
93
94    /**
95     * Retrieve the mailbox ID.
96     */
97    public static long getMailboxIdFromIntent(Intent intent) {
98        return getLongFromIntent(intent, MAILBOX_ID_PARAM);
99    }
100
101    /**
102     * Retrieve the message ID.
103     */
104    public static long getMessageIdFromIntent(Intent intent) {
105        return getLongFromIntent(intent, MESSAGE_ID_PARAM);
106    }
107
108    /**
109     * Retrieve the account UUID, or null if the UUID param is not found.
110     */
111    public static String getAccountUuidFromIntent(Intent intent) {
112        final Uri uri = intent.getData();
113        if (uri == null) {
114            return null;
115        }
116        String uuid = uri.getQueryParameter(ACCOUNT_UUID_PARAM);
117        return TextUtils.isEmpty(uuid) ? null : uuid;
118    }
119
120    private static long getLongFromIntent(Intent intent, String paramName) {
121        long value = -1;
122        if (intent.getData() != null) {
123            value = getLongParamFromUri(intent.getData(), paramName, -1);
124        }
125        return value;
126    }
127
128    private static long getLongParamFromUri(Uri uri, String paramName, long defaultValue) {
129        final String value = uri.getQueryParameter(paramName);
130        if (!TextUtils.isEmpty(value)) {
131            try {
132                return Long.parseLong(value);
133            } catch (NumberFormatException e) {
134                // return default
135            }
136        }
137        return defaultValue;
138    }
139
140    /**
141     * Create an {@link Intent} to launch an activity as the main entry point.  Existing activities
142     * will all be closed.
143     */
144    public static Intent createRestartAppIntent(Context context, Class<? extends Activity> clazz) {
145        Intent i = new Intent(context, clazz);
146        prepareRestartAppIntent(i);
147        return i;
148    }
149
150    /**
151     * Create an {@link Intent} to launch an activity as the main entry point.  Existing activities
152     * will all be closed.
153     */
154    public static Intent createRestartAppIntent(Uri data) {
155        Intent i = new Intent(Intent.ACTION_MAIN, data);
156        prepareRestartAppIntent(i);
157        return i;
158    }
159
160    private static void prepareRestartAppIntent(Intent i) {
161        i.setAction(Intent.ACTION_MAIN);
162        i.addCategory(Intent.CATEGORY_LAUNCHER);
163        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
164        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
165    }
166}
167