1 /*
2  * Copyright (C) 2008 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 
17 package com.android.email2.ui;
18 
19 import android.content.ComponentName;
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.UriMatcher;
24 import android.content.pm.PackageManager;
25 import android.database.Cursor;
26 import android.net.Uri;
27 import android.os.Bundle;
28 
29 import com.android.email.Preferences;
30 import com.android.email.provider.EmailProvider;
31 import com.android.email.service.AttachmentService;
32 import com.android.email.service.EmailServiceUtils;
33 import com.android.emailcommon.Logging;
34 import com.android.emailcommon.TempDirectory;
35 import com.android.emailcommon.provider.Mailbox;
36 import com.android.emailcommon.utility.IntentUtilities;
37 import com.android.mail.providers.Folder;
38 import com.android.mail.providers.UIProvider;
39 import com.android.mail.utils.LogTag;
40 import com.android.mail.utils.LogUtils;
41 import com.android.mail.utils.Utils;
42 
43 public class MailActivityEmail extends com.android.mail.ui.MailActivity {
44 
45     public static final String LOG_TAG = LogTag.getLogTag();
46 
47     private static final int MATCH_LEGACY_SHORTCUT_INTENT = 1;
48     /**
49      * A matcher for data URI's that specify conversation list info.
50      */
51     private static final UriMatcher sUrlMatcher = new UriMatcher(UriMatcher.NO_MATCH);
52     static {
sUrlMatcher.addURI( EmailProvider.LEGACY_AUTHORITY, "view/mailbox", MATCH_LEGACY_SHORTCUT_INTENT)53         sUrlMatcher.addURI(
54                 EmailProvider.LEGACY_AUTHORITY, "view/mailbox", MATCH_LEGACY_SHORTCUT_INTENT);
55     }
56 
57 
58     @Override
onCreate(Bundle bundle)59     public void onCreate(Bundle bundle) {
60         final Intent intent = getIntent();
61         final Uri data = intent != null ? intent.getData() : null;
62         if (data != null) {
63             final int match = sUrlMatcher.match(data);
64             switch (match) {
65                 case MATCH_LEGACY_SHORTCUT_INTENT: {
66                     final long mailboxId = IntentUtilities.getMailboxIdFromIntent(intent);
67                     final Mailbox mailbox = Mailbox.restoreMailboxWithId(this, mailboxId);
68                     if (mailbox == null) {
69                         LogUtils.e(LOG_TAG, "unable to restore mailbox");
70                         break;
71                     }
72 
73                     final Intent viewIntent = getViewIntent(mailbox.mAccountKey, mailboxId);
74                     if (viewIntent != null) {
75                         setIntent(viewIntent);
76                     }
77                     break;
78                 }
79             }
80         }
81 
82         super.onCreate(bundle);
83         TempDirectory.setTempDirectory(this);
84 
85         // Make sure all required services are running when the app is started (can prevent
86         // issues after an adb sync/install)
87         EmailProvider.setServicesEnabledAsync(this);
88     }
89 
90     /**
91      * Internal, utility method for logging.
92      * The calls to log() must be guarded with "if (Email.LOGD)" for performance reasons.
93      */
log(String message)94     public static void log(String message) {
95         LogUtils.d(Logging.LOG_TAG, message);
96     }
97 
getViewIntent(long accountId, long mailboxId)98     private Intent getViewIntent(long accountId, long mailboxId) {
99         final ContentResolver contentResolver = getContentResolver();
100 
101         final Cursor accountCursor = contentResolver.query(
102                 EmailProvider.uiUri("uiaccount", accountId),
103                 UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES,
104                 null, null, null);
105 
106         if (accountCursor == null) {
107             LogUtils.e(LOG_TAG, "Null account cursor for mAccountId %d", accountId);
108             return null;
109         }
110 
111         com.android.mail.providers.Account account = null;
112         try {
113             if (accountCursor.moveToFirst()) {
114                 account = com.android.mail.providers.Account.builder().buildFrom(accountCursor);
115             }
116         } finally {
117             accountCursor.close();
118         }
119 
120 
121         final Cursor folderCursor = contentResolver.query(
122                 EmailProvider.uiUri("uifolder", mailboxId),
123                 UIProvider.FOLDERS_PROJECTION, null, null, null);
124 
125         if (folderCursor == null) {
126             LogUtils.e(LOG_TAG, "Null folder cursor for account %d, mailbox %d",
127                     accountId, mailboxId);
128             return null;
129         }
130 
131         Folder folder = null;
132         try {
133             if (folderCursor.moveToFirst()) {
134                 folder = new Folder(folderCursor);
135             } else {
136                 LogUtils.e(LOG_TAG, "Empty folder cursor for account %d, mailbox %d",
137                         accountId, mailboxId);
138                 return null;
139             }
140         } finally {
141             folderCursor.close();
142         }
143 
144         return Utils.createViewFolderIntent(this, folder.folderUri.fullUri, account);
145     }
146 }
147