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.content.res.Configuration; 21 import android.graphics.PorterDuff; 22 import android.os.Bundle; 23 import android.support.v4.content.ContextCompat; 24 import android.view.Gravity; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.view.ViewGroup; 29 import android.widget.Button; 30 import android.widget.ImageView; 31 import android.widget.ProgressBar; 32 import android.widget.TextView; 33 34 import com.android.contacts.R; 35 import com.android.contacts.activities.ActionBarAdapter.TabState; 36 import com.android.contacts.compat.ProviderStatusCompat; 37 38 /** 39 * Fragment shown when contacts are unavailable. It contains provider status 40 * messaging as well as instructions for the user. 41 */ 42 public class ContactsUnavailableFragment extends Fragment implements OnClickListener { 43 44 private View mView; 45 private ImageView mImageView; 46 private TextView mMessageView; 47 private Button mAddAccountButton; 48 private Button mImportContactsButton; 49 private ProgressBar mProgress; 50 private View mButtonsContainer; 51 private int mNoContactsMsgResId = -1; 52 private int mLastTab = -1; 53 54 private OnContactsUnavailableActionListener mListener; 55 56 private Integer mProviderStatus; 57 58 @Override 59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 } 62 63 @Override 64 public View onCreateView( 65 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 66 mView = inflater.inflate(R.layout.contacts_unavailable_fragment, null); 67 68 mImageView = (ImageView) mView.findViewById(R.id.empty_image); 69 70 mMessageView = (TextView) mView.findViewById(R.id.message); 71 mAddAccountButton = (Button) mView.findViewById(R.id.add_account_button); 72 mAddAccountButton.setOnClickListener(this); 73 mAddAccountButton.getBackground().setColorFilter(ContextCompat.getColor(getContext(), R 74 .color.primary_color), PorterDuff.Mode.SRC_ATOP); 75 mImportContactsButton = (Button) mView.findViewById(R.id.import_contacts_button); 76 mImportContactsButton.setOnClickListener(this); 77 mImportContactsButton.getBackground().setColorFilter(ContextCompat.getColor(getContext(), 78 R.color.primary_color), PorterDuff.Mode.SRC_ATOP); 79 mProgress = (ProgressBar) mView.findViewById(R.id.progress); 80 81 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 82 mButtonsContainer = mView.findViewById(R.id.buttons_container); 83 } 84 85 if (mProviderStatus != null) { 86 updateStatus(mProviderStatus); 87 } 88 89 return mView; 90 } 91 92 public void setOnContactsUnavailableActionListener( 93 OnContactsUnavailableActionListener listener) { 94 mListener = listener; 95 } 96 97 public void updateStatus(int providerStatus) { 98 mProviderStatus = providerStatus; 99 if (mView == null) { 100 // The view hasn't been inflated yet. 101 return; 102 } 103 if (providerStatus == ProviderStatusCompat.STATUS_EMPTY) { 104 updateViewsForEmptyStatus(); 105 } else if (providerStatus == ProviderStatusCompat.STATUS_BUSY) { 106 updateViewsForBusyStatus(R.string.upgrade_in_progress); 107 } else if (providerStatus == ProviderStatusCompat.STATUS_CHANGING_LOCALE) { 108 updateViewsForBusyStatus(R.string.locale_change_in_progress); 109 } 110 } 111 112 /** 113 * Update views in the fragment when provider status is empty. 114 */ 115 private void updateViewsForEmptyStatus() { 116 setTabInfo(mNoContactsMsgResId, mLastTab); 117 if (mLastTab == TabState.ALL) { 118 updateButtonVisibilty(View.VISIBLE); 119 } 120 mProgress.setVisibility(View.GONE); 121 } 122 123 /** 124 * Update views in the fragment when provider status is busy. 125 * 126 * @param resId resource ID of the string to show in mMessageView. 127 */ 128 private void updateViewsForBusyStatus(int resId) { 129 mMessageView.setText(resId); 130 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL); 131 mMessageView.setVisibility(View.VISIBLE); 132 updateButtonVisibilty(View.GONE); 133 mProgress.setVisibility(View.VISIBLE); 134 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 135 final ViewGroup.MarginLayoutParams lp = 136 (ViewGroup.MarginLayoutParams) mMessageView.getLayoutParams(); 137 final int marginTop = 138 (int) getResources().getDimension(R.dimen.update_contact_list_top_margin); 139 lp.setMargins(0, marginTop, 0, 0); 140 mImageView.setVisibility(View.GONE); 141 } else { 142 mImageView.setVisibility(View.INVISIBLE); 143 } 144 } 145 146 @Override 147 public void onClick(View v) { 148 if (mListener == null) { 149 return; 150 } 151 switch (v.getId()) { 152 case R.id.add_account_button: 153 mListener.onAddAccountAction(); 154 break; 155 case R.id.import_contacts_button: 156 mListener.onImportContactsFromFileAction(); 157 break; 158 } 159 } 160 161 /** 162 * Set the message to be shown if no data is available for the selected tab 163 * 164 * @param resId - String resource ID of the message , -1 means view will not be visible 165 */ 166 public void setTabInfo(int resId, int callerTab) { 167 mNoContactsMsgResId = resId; 168 mLastTab = callerTab; 169 if ((mMessageView != null) && (mProviderStatus != null) && 170 mProviderStatus.equals(ProviderStatusCompat.STATUS_EMPTY)) { 171 if (resId != -1) { 172 mMessageView.setText(mNoContactsMsgResId); 173 mMessageView.setGravity(Gravity.CENTER_HORIZONTAL); 174 mMessageView.setVisibility(View.VISIBLE); 175 if (callerTab == TabState.FAVORITES) { 176 mImageView.setImageResource(R.drawable.ic_star_black_128dp); 177 mProgress.setVisibility(View.GONE); 178 updateButtonVisibilty(View.GONE); 179 } else if (callerTab == TabState.ALL) { 180 mImageView.setImageResource(R.drawable.ic_person_black_128dp); 181 updateButtonVisibilty(View.VISIBLE); 182 } 183 } else { 184 mMessageView.setVisibility(View.GONE); 185 } 186 } 187 } 188 189 private void updateButtonVisibilty(int visibility) { 190 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 191 mAddAccountButton.setVisibility(visibility); 192 mImportContactsButton.setVisibility(visibility); 193 mButtonsContainer.setVisibility(visibility); 194 } else { 195 mAddAccountButton.setVisibility(visibility); 196 mImportContactsButton.setVisibility(visibility); 197 } 198 } 199 200 @Override 201 public Context getContext() { 202 return getActivity(); 203 } 204 } 205