Home | History | Annotate | Download | only in list
      1 /*
      2  * Copyright (C) 2010 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 package com.android.contacts.list;
     17 
     18 import android.app.Fragment;
     19 import android.content.Context;
     20 import android.os.Bundle;
     21 import android.provider.ContactsContract.ProviderStatus;
     22 import android.view.Gravity;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.View.OnClickListener;
     26 import android.view.ViewGroup;
     27 import android.widget.Button;
     28 import android.widget.ProgressBar;
     29 import android.widget.TextView;
     30 
     31 import com.android.contacts.R;
     32 
     33 /**
     34  * Fragment shown when contacts are unavailable. It contains provider status
     35  * messaging as well as instructions for the user.
     36  */
     37 public class ContactsUnavailableFragment extends Fragment implements OnClickListener {
     38 
     39     private View mView;
     40     private TextView mMessageView;
     41     private TextView mSecondaryMessageView;
     42     private Button mCreateContactButton;
     43     private Button mAddAccountButton;
     44     private Button mImportContactsButton;
     45     private Button mUninstallAppsButton;
     46     private Button mRetryUpgradeButton;
     47     private ProgressBar mProgress;
     48     private int mNoContactsMsgResId = -1;
     49     private int mNSecNoContactsMsgResId = -1;
     50 
     51     private OnContactsUnavailableActionListener mListener;
     52 
     53     private ProviderStatusWatcher.Status mProviderStatus;
     54 
     55     @Override
     56     public void onCreate(Bundle savedInstanceState) {
     57         super.onCreate(savedInstanceState);
     58     }
     59 
     60     @Override
     61     public View onCreateView(
     62             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     63         mView = inflater.inflate(R.layout.contacts_unavailable_fragment, null);
     64         mMessageView = (TextView) mView.findViewById(R.id.message);
     65         mSecondaryMessageView = (TextView) mView.findViewById(R.id.secondary_message);
     66         mCreateContactButton = (Button) mView.findViewById(R.id.create_contact_button);
     67         mCreateContactButton.setOnClickListener(this);
     68         mAddAccountButton = (Button) mView.findViewById(R.id.add_account_button);
     69         mAddAccountButton.setOnClickListener(this);
     70         mImportContactsButton = (Button) mView.findViewById(R.id.import_contacts_button);
     71         mImportContactsButton.setOnClickListener(this);
     72         mUninstallAppsButton = (Button) mView.findViewById(R.id.import_failure_uninstall_button);
     73         mUninstallAppsButton.setOnClickListener(this);
     74         mRetryUpgradeButton = (Button) mView.findViewById(R.id.import_failure_retry_button);
     75         mRetryUpgradeButton.setOnClickListener(this);
     76         mProgress = (ProgressBar) mView.findViewById(R.id.progress);
     77 
     78         if (mProviderStatus != null) {
     79             updateStatus(mProviderStatus);
     80         }
     81 
     82         return mView;
     83     }
     84 
     85     public void setOnContactsUnavailableActionListener(
     86             OnContactsUnavailableActionListener listener) {
     87         mListener = listener;
     88     }
     89 
     90     public void updateStatus(ProviderStatusWatcher.Status providerStatus) {
     91         mProviderStatus = providerStatus;
     92         if (mView == null) {
     93             // The view hasn't been inflated yet.
     94             return;
     95         }
     96         switch (providerStatus.status) {
     97             case ProviderStatus.STATUS_NO_ACCOUNTS_NO_CONTACTS:
     98                 setMessageText(mNoContactsMsgResId, mNSecNoContactsMsgResId);
     99                 mCreateContactButton.setVisibility(View.VISIBLE);
    100                 mAddAccountButton.setVisibility(View.VISIBLE);
    101                 mImportContactsButton.setVisibility(View.VISIBLE);
    102                 mUninstallAppsButton.setVisibility(View.GONE);
    103                 mRetryUpgradeButton.setVisibility(View.GONE);
    104                 mProgress.setVisibility(View.GONE);
    105                 break;
    106 
    107             case ProviderStatus.STATUS_CHANGING_LOCALE:
    108                 mMessageView.setText(R.string.locale_change_in_progress);
    109                 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
    110                 mMessageView.setVisibility(View.VISIBLE);
    111                 mCreateContactButton.setVisibility(View.GONE);
    112                 mAddAccountButton.setVisibility(View.GONE);
    113                 mImportContactsButton.setVisibility(View.GONE);
    114                 mUninstallAppsButton.setVisibility(View.GONE);
    115                 mRetryUpgradeButton.setVisibility(View.GONE);
    116                 mProgress.setVisibility(View.VISIBLE);
    117                 break;
    118 
    119             case ProviderStatus.STATUS_UPGRADING:
    120                 mMessageView.setText(R.string.upgrade_in_progress);
    121                 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
    122                 mMessageView.setVisibility(View.VISIBLE);
    123                 mCreateContactButton.setVisibility(View.GONE);
    124                 mAddAccountButton.setVisibility(View.GONE);
    125                 mImportContactsButton.setVisibility(View.GONE);
    126                 mUninstallAppsButton.setVisibility(View.GONE);
    127                 mRetryUpgradeButton.setVisibility(View.GONE);
    128                 mProgress.setVisibility(View.VISIBLE);
    129                 break;
    130 
    131             case ProviderStatus.STATUS_UPGRADE_OUT_OF_MEMORY:
    132                 String message = getResources().getString(R.string.upgrade_out_of_memory,
    133                         new Object[] { providerStatus.data});
    134                 mMessageView.setText(message);
    135                 mMessageView.setGravity(Gravity.START);
    136                 mMessageView.setVisibility(View.VISIBLE);
    137                 mCreateContactButton.setVisibility(View.GONE);
    138                 mAddAccountButton.setVisibility(View.GONE);
    139                 mImportContactsButton.setVisibility(View.GONE);
    140                 mUninstallAppsButton.setVisibility(View.VISIBLE);
    141                 mRetryUpgradeButton.setVisibility(View.VISIBLE);
    142                 mProgress.setVisibility(View.GONE);
    143                 break;
    144         }
    145     }
    146 
    147     @Override
    148     public void onClick(View v) {
    149         if (mListener == null) {
    150             return;
    151         }
    152         switch (v.getId()) {
    153             case R.id.create_contact_button:
    154                 mListener.onCreateNewContactAction();
    155                 break;
    156             case R.id.add_account_button:
    157                 mListener.onAddAccountAction();
    158                 break;
    159             case R.id.import_contacts_button:
    160                 mListener.onImportContactsFromFileAction();
    161                 break;
    162             case R.id.import_failure_uninstall_button:
    163                 mListener.onFreeInternalStorageAction();
    164                 break;
    165             case R.id.import_failure_retry_button:
    166                 final Context context = getActivity();
    167                 if (context != null) { // Just in case.
    168                     ProviderStatusWatcher.retryUpgrade(context);
    169                 }
    170                 break;
    171         }
    172     }
    173     /**
    174      * Set the message to be shown if no data is available for the selected tab
    175      *
    176      * @param resId - String resource ID of the message , -1 means view will not be visible
    177      */
    178     public void setMessageText(int resId, int secResId) {
    179         mNoContactsMsgResId = resId;
    180         mNSecNoContactsMsgResId = secResId;
    181         if ((mMessageView != null) && (mProviderStatus != null) &&
    182                 (mProviderStatus.status == ProviderStatus.STATUS_NO_ACCOUNTS_NO_CONTACTS)) {
    183             if (resId != -1) {
    184                 mMessageView.setText(mNoContactsMsgResId);
    185                 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
    186                 mMessageView.setVisibility(View.VISIBLE);
    187                 if (secResId != -1) {
    188                     mSecondaryMessageView.setText(mNSecNoContactsMsgResId);
    189                     mSecondaryMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
    190                     mSecondaryMessageView.setVisibility(View.VISIBLE);
    191                 } else {
    192                     mSecondaryMessageView.setVisibility(View.INVISIBLE);
    193                 }
    194             } else {
    195                 mSecondaryMessageView.setVisibility(View.GONE);
    196                 mMessageView.setVisibility(View.GONE);
    197             }
    198         }
    199     }
    200 }
    201