Home | History | Annotate | Download | only in setup
      1 /*
      2  * Copyright (C) 2014 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.email.activity.setup;
     18 
     19 import android.app.Fragment;
     20 import android.app.LoaderManager;
     21 import android.content.ContentValues;
     22 import android.content.Context;
     23 import android.content.Loader;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 
     27 import com.android.email.provider.AccountBackupRestore;
     28 import com.android.emailcommon.provider.Account;
     29 import com.android.emailcommon.provider.EmailContent;
     30 import com.android.mail.ui.MailAsyncTaskLoader;
     31 
     32 public class AccountFinalizeFragment extends Fragment {
     33     public static final String TAG = "AccountFinalizeFragment";
     34 
     35     private static final String ACCOUNT_TAG = "account";
     36 
     37     private static final int FINAL_ACCOUNT_TASK_LOADER_ID = 0;
     38 
     39     private Context mAppContext;
     40     private final Handler mHandler = new Handler();
     41 
     42     public interface Callback {
     43         void onAccountFinalizeFragmentComplete();
     44     }
     45 
     46     public AccountFinalizeFragment() {}
     47 
     48     public static AccountFinalizeFragment newInstance(Account account) {
     49         final AccountFinalizeFragment f = new AccountFinalizeFragment();
     50         final Bundle args = new Bundle(1);
     51         args.putParcelable(ACCOUNT_TAG, account);
     52         f.setArguments(args);
     53         return f;
     54     }
     55 
     56     @Override
     57     public void onCreate(Bundle savedInstanceState) {
     58         super.onCreate(savedInstanceState);
     59 
     60         mAppContext = getActivity().getApplicationContext();
     61 
     62         setRetainInstance(true);
     63     }
     64 
     65     @Override
     66     public void onResume() {
     67         super.onResume();
     68 
     69         getLoaderManager().initLoader(FINAL_ACCOUNT_TASK_LOADER_ID, getArguments(),
     70                 new LoaderManager.LoaderCallbacks<Boolean>() {
     71                     @Override
     72                     public Loader<Boolean> onCreateLoader(int id, Bundle args) {
     73                         final Account accountArg = args.getParcelable(ACCOUNT_TAG);
     74                         return new FinalSetupTaskLoader(mAppContext, accountArg);
     75                     }
     76 
     77                     @Override
     78                     public void onLoadFinished(Loader<Boolean> loader, Boolean success) {
     79                         if (!success) {
     80                             return;
     81                         }
     82                         mHandler.post(new Runnable() {
     83                             @Override
     84                             public void run() {
     85                                 if (isResumed()) {
     86                                     Callback activity = (Callback) getActivity();
     87                                     activity.onAccountFinalizeFragmentComplete();
     88                                 }
     89                             }
     90                         });
     91 
     92                     }
     93 
     94                     @Override
     95                     public void onLoaderReset(Loader<Boolean> loader) {}
     96                 });
     97     }
     98 
     99     /**
    100      * Final account setup work is handled in this Loader:
    101      *   Commit final values to provider
    102      *   Trigger account backup
    103      */
    104     private static class FinalSetupTaskLoader extends MailAsyncTaskLoader<Boolean> {
    105 
    106         private final Account mAccount;
    107 
    108         public FinalSetupTaskLoader(Context context, Account account) {
    109             super(context);
    110             mAccount = account;
    111         }
    112 
    113         Account getAccount() {
    114             return mAccount;
    115         }
    116 
    117         @Override
    118         public Boolean loadInBackground() {
    119             // Update the account in the database
    120             final ContentValues cv = new ContentValues();
    121             cv.put(EmailContent.AccountColumns.DISPLAY_NAME, mAccount.getDisplayName());
    122             cv.put(EmailContent.AccountColumns.SENDER_NAME, mAccount.getSenderName());
    123             mAccount.update(getContext(), cv);
    124 
    125             // Update the backup (side copy) of the accounts
    126             AccountBackupRestore.backup(getContext());
    127 
    128             return true;
    129         }
    130 
    131         @Override
    132         protected void onDiscardResult(Boolean result) {}
    133     }
    134 }
    135