Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 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 | |
Brian Attwell | a3e498a | 2014-10-27 10:46:24 -0700 | [diff] [blame] | 18 | import android.app.Fragment; |
Wenyi Wang | 9bc9ba8 | 2015-11-17 19:37:33 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 21 | import android.content.res.Configuration; |
Wenyi Wang | b0cfae5 | 2015-09-28 17:17:40 -0700 | [diff] [blame] | 22 | import android.graphics.PorterDuff; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 23 | import android.os.Bundle; |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 24 | import android.provider.ContactsContract.ProviderStatus; |
Wenyi Wang | b0cfae5 | 2015-09-28 17:17:40 -0700 | [diff] [blame] | 25 | import android.support.v4.content.ContextCompat; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 26 | import android.view.Gravity; |
| 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
| 29 | import android.view.View.OnClickListener; |
| 30 | import android.view.ViewGroup; |
| 31 | import android.widget.Button; |
Wenyi Wang | b0cfae5 | 2015-09-28 17:17:40 -0700 | [diff] [blame] | 32 | import android.widget.ImageView; |
yaolu | 189036a | 2016-07-29 15:50:28 -0700 | [diff] [blame] | 33 | import android.widget.LinearLayout; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 34 | import android.widget.ProgressBar; |
| 35 | import android.widget.TextView; |
| 36 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 37 | import com.android.contacts.R; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 38 | import com.android.contacts.common.compat.ProviderStatusCompat; |
James Laskey | 1e2102f | 2016-09-19 10:09:08 -0700 | [diff] [blame] | 39 | import com.android.contacts.common.interactions.ImportDialogFragment; |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 40 | import com.android.contacts.common.util.ImplicitIntentsUtil; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 41 | |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Fragment shown when contacts are unavailable. It contains provider status |
| 44 | * messaging as well as instructions for the user. |
| 45 | */ |
Brian Attwell | a3e498a | 2014-10-27 10:46:24 -0700 | [diff] [blame] | 46 | public class ContactsUnavailableFragment extends Fragment implements OnClickListener { |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 47 | |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 48 | private View mView; |
Wenyi Wang | b0cfae5 | 2015-09-28 17:17:40 -0700 | [diff] [blame] | 49 | private ImageView mImageView; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 50 | private TextView mMessageView; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 51 | private Button mAddAccountButton; |
| 52 | private Button mImportContactsButton; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 53 | private ProgressBar mProgress; |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 54 | private View mButtonsContainer; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 55 | |
Brian Attwell | c3d202c | 2015-01-08 15:01:03 -0800 | [diff] [blame] | 56 | private Integer mProviderStatus; |
Makoto Onuki | d8fa716 | 2012-05-16 13:45:48 -0700 | [diff] [blame] | 57 | |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 58 | @Override |
| 59 | public View onCreateView( |
| 60 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 61 | mView = inflater.inflate(R.layout.contacts_unavailable_fragment, null); |
Wenyi Wang | b0cfae5 | 2015-09-28 17:17:40 -0700 | [diff] [blame] | 62 | |
| 63 | mImageView = (ImageView) mView.findViewById(R.id.empty_image); |
yaolu | 189036a | 2016-07-29 15:50:28 -0700 | [diff] [blame] | 64 | final LinearLayout.LayoutParams layoutParams = |
| 65 | (LinearLayout.LayoutParams) mImageView.getLayoutParams(); |
| 66 | final int screenHeight = getResources().getDisplayMetrics().heightPixels; |
| 67 | final int topMargin = |
| 68 | screenHeight / getResources() |
| 69 | .getInteger(R.integer.contacts_no_account_empty_image_margin_divisor) |
| 70 | - getResources() |
| 71 | .getDimensionPixelSize(R.dimen.contacts_no_account_empty_image_offset); |
| 72 | layoutParams.setMargins(0, topMargin, 0, 0); |
| 73 | layoutParams.gravity = Gravity.CENTER_HORIZONTAL; |
| 74 | mImageView.setLayoutParams(layoutParams); |
Wenyi Wang | b0cfae5 | 2015-09-28 17:17:40 -0700 | [diff] [blame] | 75 | |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 76 | mMessageView = (TextView) mView.findViewById(R.id.message); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 77 | mAddAccountButton = (Button) mView.findViewById(R.id.add_account_button); |
| 78 | mAddAccountButton.setOnClickListener(this); |
Wenyi Wang | b0cfae5 | 2015-09-28 17:17:40 -0700 | [diff] [blame] | 79 | mAddAccountButton.getBackground().setColorFilter(ContextCompat.getColor(getContext(), R |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 80 | .color.primary_color), PorterDuff.Mode.SRC_ATOP); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 81 | mImportContactsButton = (Button) mView.findViewById(R.id.import_contacts_button); |
| 82 | mImportContactsButton.setOnClickListener(this); |
Wenyi Wang | b0cfae5 | 2015-09-28 17:17:40 -0700 | [diff] [blame] | 83 | mImportContactsButton.getBackground().setColorFilter(ContextCompat.getColor(getContext(), |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 84 | R.color.primary_color), PorterDuff.Mode.SRC_ATOP); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 85 | mProgress = (ProgressBar) mView.findViewById(R.id.progress); |
Makoto Onuki | d8fa716 | 2012-05-16 13:45:48 -0700 | [diff] [blame] | 86 | |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 87 | if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 88 | mButtonsContainer = mView.findViewById(R.id.buttons_container); |
| 89 | } |
| 90 | |
Makoto Onuki | d8fa716 | 2012-05-16 13:45:48 -0700 | [diff] [blame] | 91 | if (mProviderStatus != null) { |
| 92 | updateStatus(mProviderStatus); |
| 93 | } |
| 94 | |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 95 | return mView; |
| 96 | } |
| 97 | |
Brian Attwell | c3d202c | 2015-01-08 15:01:03 -0800 | [diff] [blame] | 98 | public void updateStatus(int providerStatus) { |
Makoto Onuki | d8fa716 | 2012-05-16 13:45:48 -0700 | [diff] [blame] | 99 | mProviderStatus = providerStatus; |
| 100 | if (mView == null) { |
| 101 | // The view hasn't been inflated yet. |
| 102 | return; |
| 103 | } |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 104 | if (providerStatus == ProviderStatusCompat.STATUS_EMPTY) { |
| 105 | updateViewsForEmptyStatus(); |
| 106 | } else if (providerStatus == ProviderStatusCompat.STATUS_BUSY) { |
| 107 | updateViewsForBusyStatus(R.string.upgrade_in_progress); |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 108 | } else if (providerStatus == ProviderStatusCompat.STATUS_CHANGING_LOCALE) { |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 109 | updateViewsForBusyStatus(R.string.locale_change_in_progress); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 113 | /** |
| 114 | * Update views in the fragment when provider status is empty. |
| 115 | */ |
| 116 | private void updateViewsForEmptyStatus() { |
yaolu | c67c7dc | 2016-10-17 12:19:37 -0700 | [diff] [blame^] | 117 | mMessageView.setText(R.string.noContacts); |
| 118 | mMessageView.setVisibility(View.VISIBLE); |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 119 | updateButtonVisibility(View.VISIBLE); |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 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); |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 130 | mMessageView.setVisibility(View.VISIBLE); |
yaolu | 189036a | 2016-07-29 15:50:28 -0700 | [diff] [blame] | 131 | mImageView.setVisibility(View.GONE); |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 132 | updateButtonVisibility(View.GONE); |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 133 | mProgress.setVisibility(View.VISIBLE); |
yaolu | 189036a | 2016-07-29 15:50:28 -0700 | [diff] [blame] | 134 | |
| 135 | final ViewGroup.MarginLayoutParams layoutParams = |
| 136 | (ViewGroup.MarginLayoutParams) mMessageView.getLayoutParams(); |
| 137 | final int marginTop = |
| 138 | (int) getResources().getDimension(R.dimen.update_contact_list_top_margin); |
| 139 | layoutParams.setMargins(0, marginTop, 0, 0); |
| 140 | mMessageView.setGravity(Gravity.CENTER_HORIZONTAL); |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 143 | @Override |
| 144 | public void onClick(View v) { |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 145 | switch (v.getId()) { |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 146 | case R.id.add_account_button: |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 147 | final Intent intent = ImplicitIntentsUtil.getIntentForAddingGoogleAccount(); |
| 148 | ImplicitIntentsUtil.startActivityOutsideApp(getActivity(), intent); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 149 | break; |
| 150 | case R.id.import_contacts_button: |
James Laskey | 1e2102f | 2016-09-19 10:09:08 -0700 | [diff] [blame] | 151 | ImportDialogFragment.show(getFragmentManager(), getActivity().getClass()); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 152 | break; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 155 | |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 156 | private boolean areContactsAvailable() { |
| 157 | return (mProviderStatus != null) && mProviderStatus.equals(ProviderStatus.STATUS_NORMAL); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | private void updateButtonVisibility(int visibility) { |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 162 | if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 163 | mAddAccountButton.setVisibility(visibility); |
| 164 | mImportContactsButton.setVisibility(visibility); |
| 165 | mButtonsContainer.setVisibility(visibility); |
| 166 | } else { |
| 167 | mAddAccountButton.setVisibility(visibility); |
| 168 | mImportContactsButton.setVisibility(visibility); |
| 169 | } |
| 170 | } |
| 171 | |
Wenyi Wang | 9bc9ba8 | 2015-11-17 19:37:33 -0800 | [diff] [blame] | 172 | @Override |
| 173 | public Context getContext() { |
| 174 | return getActivity(); |
| 175 | } |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 176 | } |