Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2012 Google Inc.
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.mail.ui;
     19 
     20 import android.app.Fragment;
     21 import android.app.LoaderManager;
     22 import android.content.CursorLoader;
     23 import android.content.Intent;
     24 import android.content.Loader;
     25 import android.database.Cursor;
     26 import android.os.Bundle;
     27 import android.provider.Settings;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 
     32 import com.android.mail.R;
     33 import com.android.mail.providers.Account;
     34 import com.android.mail.providers.UIProvider.SyncStatus;
     35 
     36 public class WaitFragment extends Fragment implements View.OnClickListener,
     37         LoaderManager.LoaderCallbacks<Cursor> {
     38     // Keys used to pass data to {@link WaitFragment}.
     39     private static final String ACCOUNT_KEY = "account";
     40 
     41     private static final String DEFAULT_KEY = "isDefault";
     42 
     43     private static final int MANUAL_SYNC_LOADER = 0;
     44 
     45 
     46     private Account mAccount;
     47 
     48     private LayoutInflater mInflater;
     49 
     50     private boolean mDefault;
     51 
     52     // Public no-args constructor needed for fragment re-instantiation
     53     public WaitFragment() {}
     54 
     55     public static WaitFragment newInstance(Account account) {
     56         return newInstance(account, false);
     57     }
     58 
     59     public static WaitFragment newInstance(Account account, boolean def) {
     60         WaitFragment fragment = new WaitFragment();
     61 
     62         final Bundle args = new Bundle();
     63         args.putParcelable(ACCOUNT_KEY, account);
     64         args.putBoolean(DEFAULT_KEY, def);
     65         fragment.setArguments(args);
     66         return fragment;
     67     }
     68 
     69     @Override
     70     public void onCreate(Bundle savedInstanceState) {
     71         super.onCreate(savedInstanceState);
     72 
     73         Bundle args = getArguments();
     74         mAccount = (Account)args.getParcelable(ACCOUNT_KEY);
     75         mDefault = args.getBoolean(DEFAULT_KEY, false);
     76     }
     77 
     78     @Override
     79     public View onCreateView(
     80             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     81         mInflater = inflater;
     82         ViewGroup wrapper = (ViewGroup) mInflater
     83                 .inflate(R.layout.wait_container, container, false);
     84         wrapper.addView(getContent(wrapper));
     85         return wrapper;
     86     }
     87 
     88     private View getContent(ViewGroup root) {
     89         final View view;
     90         if (mAccount != null
     91                 && (mAccount.syncStatus & SyncStatus.MANUAL_SYNC_REQUIRED)
     92                     == SyncStatus.MANUAL_SYNC_REQUIRED) {
     93             // A manual sync is required
     94             view = mInflater.inflate(R.layout.wait_for_manual_sync, root, false);
     95 
     96             view.findViewById(R.id.manual_sync).setOnClickListener(this);
     97             view.findViewById(R.id.change_sync_settings).setOnClickListener(this);
     98 
     99         } else if (mDefault) {
    100             view = mInflater.inflate(R.layout.wait_default, root, false);
    101         } else {
    102             view = mInflater.inflate(R.layout.wait_for_sync, root, false);
    103         }
    104 
    105         return view;
    106     }
    107 
    108     public void updateAccount(Account account) {
    109         mAccount = account;
    110         ViewGroup parent = (ViewGroup) getView();
    111         if (parent != null) {
    112             parent.removeAllViews();
    113             parent.addView(getContent(parent));
    114         }
    115     }
    116 
    117     Account getAccount() {
    118         return mAccount;
    119     }
    120 
    121     @Override
    122     public void onClick(View v) {
    123         final int id = v.getId();
    124 
    125         if (id == R.id.change_sync_settings) {
    126             Intent intent = new Intent(Settings.ACTION_SYNC_SETTINGS);
    127             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    128             startActivity(intent);
    129         } else if (id == R.id.manual_sync) {
    130             if (mAccount != null && mAccount.manualSyncUri != null) {
    131                 getLoaderManager().initLoader(MANUAL_SYNC_LOADER, null, this);
    132             }
    133         }
    134     }
    135 
    136     @Override
    137     public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
    138         // Tell the account to sync manually. As a side effect, updates will come into the
    139         // controller for this fragment and update it as necessary.
    140         return new CursorLoader(getActivity(), mAccount.manualSyncUri, null, null, null, null);
    141     }
    142 
    143     @Override
    144     public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    145         // Do nothing.
    146     }
    147 
    148     @Override
    149     public void onLoaderReset(Loader<Cursor> arg0) {
    150         // Do nothing.
    151     }
    152 }