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(); |
yaolu | 8b3a1d8 | 2016-10-18 09:35:12 -0700 | [diff] [blame] | 106 | } else if (providerStatus == ProviderStatusCompat.STATUS_BUSY |
| 107 | || providerStatus == ProviderStatusCompat.STATUS_CHANGING_LOCALE) { |
| 108 | updateViewsForBusyStatus(); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Update views in the fragment when provider status is empty. |
| 114 | */ |
| 115 | private void updateViewsForEmptyStatus() { |
yaolu | c67c7dc | 2016-10-17 12:19:37 -0700 | [diff] [blame] | 116 | mMessageView.setVisibility(View.VISIBLE); |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 117 | updateButtonVisibility(View.VISIBLE); |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 118 | mProgress.setVisibility(View.GONE); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Update views in the fragment when provider status is busy. |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 123 | */ |
yaolu | 8b3a1d8 | 2016-10-18 09:35:12 -0700 | [diff] [blame] | 124 | private void updateViewsForBusyStatus() { |
| 125 | mMessageView.setVisibility(View.GONE); |
yaolu | 189036a | 2016-07-29 15:50:28 -0700 | [diff] [blame] | 126 | mImageView.setVisibility(View.GONE); |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 127 | updateButtonVisibility(View.GONE); |
Wenyi Wang | 2795a88 | 2015-11-23 16:26:12 -0800 | [diff] [blame] | 128 | mProgress.setVisibility(View.VISIBLE); |
| 129 | } |
| 130 | |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 131 | @Override |
| 132 | public void onClick(View v) { |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 133 | switch (v.getId()) { |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 134 | case R.id.add_account_button: |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 135 | final Intent intent = ImplicitIntentsUtil.getIntentForAddingGoogleAccount(); |
| 136 | ImplicitIntentsUtil.startActivityOutsideApp(getActivity(), intent); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 137 | break; |
| 138 | case R.id.import_contacts_button: |
Marcus Hagerott | 02f5dba | 2016-10-18 12:34:57 -0700 | [diff] [blame] | 139 | ImportDialogFragment.show(getFragmentManager()); |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 140 | break; |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 141 | } |
| 142 | } |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 143 | |
Wenyi Wang | 46dff68 | 2016-09-15 18:10:33 -0700 | [diff] [blame] | 144 | private void updateButtonVisibility(int visibility) { |
Wenyi Wang | f74276c | 2015-11-30 15:41:58 -0800 | [diff] [blame] | 145 | if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { |
| 146 | mAddAccountButton.setVisibility(visibility); |
| 147 | mImportContactsButton.setVisibility(visibility); |
| 148 | mButtonsContainer.setVisibility(visibility); |
| 149 | } else { |
| 150 | mAddAccountButton.setVisibility(visibility); |
| 151 | mImportContactsButton.setVisibility(visibility); |
| 152 | } |
| 153 | } |
| 154 | |
Wenyi Wang | 9bc9ba8 | 2015-11-17 19:37:33 -0800 | [diff] [blame] | 155 | @Override |
| 156 | public Context getContext() { |
| 157 | return getActivity(); |
| 158 | } |
Dmitri Plotnikov | 8b82646 | 2010-11-17 14:52:24 -0800 | [diff] [blame] | 159 | } |