• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 ProgressBar mProgress;
46     private int mNoContactsMsgResId = -1;
47     private int mNSecNoContactsMsgResId = -1;
48 
49     private OnContactsUnavailableActionListener mListener;
50 
51     private Integer mProviderStatus;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     public void onCreate(Bundle savedInstanceState) {
55         super.onCreate(savedInstanceState);
56     }
57 
58     @Override
onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)59     public View onCreateView(
60             LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
61         mView = inflater.inflate(R.layout.contacts_unavailable_fragment, null);
62         mMessageView = (TextView) mView.findViewById(R.id.message);
63         mSecondaryMessageView = (TextView) mView.findViewById(R.id.secondary_message);
64         mCreateContactButton = (Button) mView.findViewById(R.id.create_contact_button);
65         mCreateContactButton.setOnClickListener(this);
66         mAddAccountButton = (Button) mView.findViewById(R.id.add_account_button);
67         mAddAccountButton.setOnClickListener(this);
68         mImportContactsButton = (Button) mView.findViewById(R.id.import_contacts_button);
69         mImportContactsButton.setOnClickListener(this);
70         mProgress = (ProgressBar) mView.findViewById(R.id.progress);
71 
72         if (mProviderStatus != null) {
73             updateStatus(mProviderStatus);
74         }
75 
76         return mView;
77     }
78 
setOnContactsUnavailableActionListener( OnContactsUnavailableActionListener listener)79     public void setOnContactsUnavailableActionListener(
80             OnContactsUnavailableActionListener listener) {
81         mListener = listener;
82     }
83 
updateStatus(int providerStatus)84     public void updateStatus(int providerStatus) {
85         mProviderStatus = providerStatus;
86         if (mView == null) {
87             // The view hasn't been inflated yet.
88             return;
89         }
90         switch (providerStatus) {
91             case ProviderStatus.STATUS_EMPTY:
92                 setMessageText(mNoContactsMsgResId, mNSecNoContactsMsgResId);
93                 mCreateContactButton.setVisibility(View.VISIBLE);
94                 mAddAccountButton.setVisibility(View.VISIBLE);
95                 mImportContactsButton.setVisibility(View.VISIBLE);
96                 mProgress.setVisibility(View.GONE);
97                 break;
98 
99             case ProviderStatus.STATUS_BUSY:
100                 mMessageView.setText(R.string.upgrade_in_progress);
101                 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
102                 mMessageView.setVisibility(View.VISIBLE);
103                 mCreateContactButton.setVisibility(View.GONE);
104                 mAddAccountButton.setVisibility(View.GONE);
105                 mImportContactsButton.setVisibility(View.GONE);
106                 mProgress.setVisibility(View.VISIBLE);
107                 break;
108         }
109     }
110 
111     @Override
onClick(View v)112     public void onClick(View v) {
113         if (mListener == null) {
114             return;
115         }
116         switch (v.getId()) {
117             case R.id.create_contact_button:
118                 mListener.onCreateNewContactAction();
119                 break;
120             case R.id.add_account_button:
121                 mListener.onAddAccountAction();
122                 break;
123             case R.id.import_contacts_button:
124                 mListener.onImportContactsFromFileAction();
125                 break;
126         }
127     }
128     /**
129      * Set the message to be shown if no data is available for the selected tab
130      *
131      * @param resId - String resource ID of the message , -1 means view will not be visible
132      */
setMessageText(int resId, int secResId)133     public void setMessageText(int resId, int secResId) {
134         mNoContactsMsgResId = resId;
135         mNSecNoContactsMsgResId = secResId;
136         if ((mMessageView != null) && (mProviderStatus != null) &&
137                 (mProviderStatus.equals(ProviderStatus.STATUS_EMPTY))) {
138             if (resId != -1) {
139                 mMessageView.setText(mNoContactsMsgResId);
140                 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
141                 mMessageView.setVisibility(View.VISIBLE);
142                 if (secResId != -1) {
143                     mSecondaryMessageView.setText(mNSecNoContactsMsgResId);
144                     mSecondaryMessageView.setGravity(Gravity.CENTER_HORIZONTAL);
145                     mSecondaryMessageView.setVisibility(View.VISIBLE);
146                 } else {
147                     mSecondaryMessageView.setVisibility(View.INVISIBLE);
148                 }
149             } else {
150                 mSecondaryMessageView.setVisibility(View.GONE);
151                 mMessageView.setVisibility(View.GONE);
152             }
153         }
154     }
155 }
156