1 package com.android.email.activity.setup;
2 
3 import android.app.Activity;
4 import android.app.LoaderManager;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.Loader;
8 import android.net.Uri;
9 import android.os.AsyncTask;
10 import android.os.Bundle;
11 
12 import com.android.emailcommon.provider.Account;
13 import com.android.emailcommon.provider.EmailContent;
14 import com.android.emailcommon.utility.IntentUtilities;
15 import com.android.mail.ui.MailAsyncTaskLoader;
16 
17 /**
18  * This activity is headless. It exists to load the Account object from  the given account ID and
19  * then starts the {@link AccountServerSettingsActivity} activity with the incoming/outgoing
20  * settings fragment
21  */
22 public class HeadlessAccountSettingsLoader extends Activity {
23 
getOutgoingSettingsUri(long accountId)24     public static Uri getOutgoingSettingsUri(long accountId) {
25         final Uri.Builder baseUri = Uri.parse("auth://" + EmailContent.EMAIL_PACKAGE_NAME +
26                 ".ACCOUNT_SETTINGS/outgoing/").buildUpon();
27         IntentUtilities.setAccountId(baseUri, accountId);
28         return baseUri.build();
29     }
30 
31     @Override
onCreate(Bundle savedInstanceState)32     public void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34 
35         final Intent i = getIntent();
36         final long accountID = IntentUtilities.getAccountIdFromIntent(i);
37 
38         if (savedInstanceState == null) {
39             new LoadAccountIncomingSettingsAsyncTask(getApplicationContext(),
40                     "incoming".equals(i.getData().getLastPathSegment()))
41                     .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, accountID);
42         }
43     }
44 
45     /**
46      * Asynchronously loads the Account object from its ID and then navigates to the AccountSettings
47      * fragment.
48      */
49     private class LoadAccountIncomingSettingsAsyncTask extends AsyncTask<Long, Void, Account> {
50         private final Context mContext;
51         private final boolean mIncoming;
52 
LoadAccountIncomingSettingsAsyncTask(Context context, boolean incoming)53         private LoadAccountIncomingSettingsAsyncTask(Context context, boolean incoming) {
54             mContext = context;
55             mIncoming = incoming;
56         }
57 
doInBackground(Long... params)58         protected Account doInBackground(Long... params) {
59             return Account.restoreAccountWithId(mContext, params[0]);
60         }
61 
onPostExecute(Account result)62         protected void onPostExecute(Account result) {
63             // create an Intent to view a new activity
64             final Intent intent;
65             if (mIncoming) {
66                 intent = AccountServerSettingsActivity.getIntentForIncoming(mContext, result);
67             } else {
68                 intent = AccountServerSettingsActivity.getIntentForOutgoing(mContext, result);
69             }
70             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
71 
72             mContext.startActivity(intent);
73 
74             finish();
75          }
76     }
77 }