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 */
16package com.android.contacts.list;
17
18import android.app.Fragment;
19import android.content.Context;
20import android.content.Intent;
21import android.content.res.Configuration;
22import android.graphics.PorterDuff;
23import android.os.Bundle;
24import android.support.v4.content.ContextCompat;
25import android.view.Gravity;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.ViewGroup;
30import android.widget.Button;
31import android.widget.ImageView;
32import android.widget.LinearLayout;
33import android.widget.ProgressBar;
34import android.widget.TextView;
35
36import com.android.contacts.R;
37import com.android.contacts.compat.ProviderStatusCompat;
38import com.android.contacts.interactions.ImportDialogFragment;
39import com.android.contacts.util.ImplicitIntentsUtil;
40
41/**
42 * Fragment shown when contacts are unavailable. It contains provider status
43 * messaging as well as instructions for the user.
44 */
45public class ContactsUnavailableFragment extends Fragment implements OnClickListener {
46
47    private View mView;
48    private ImageView mImageView;
49    private TextView mMessageView;
50    private Button mAddAccountButton;
51    private Button mImportContactsButton;
52    private ProgressBar mProgress;
53    private View mButtonsContainer;
54
55    private Integer mProviderStatus;
56
57    @Override
58    public View onCreateView(
59            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
60        mView = inflater.inflate(R.layout.contacts_unavailable_fragment, null);
61
62        mImageView = (ImageView) mView.findViewById(R.id.empty_image);
63        final LinearLayout.LayoutParams layoutParams =
64                (LinearLayout.LayoutParams) mImageView.getLayoutParams();
65        final int screenHeight = getResources().getDisplayMetrics().heightPixels;
66        final int topMargin =
67                screenHeight / getResources()
68                        .getInteger(R.integer.contacts_no_account_empty_image_margin_divisor)
69                - getResources()
70                        .getDimensionPixelSize(R.dimen.contacts_no_account_empty_image_offset);
71        layoutParams.setMargins(0, topMargin, 0, 0);
72        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
73        mImageView.setLayoutParams(layoutParams);
74
75        mMessageView = (TextView) mView.findViewById(R.id.message);
76        mAddAccountButton = (Button) mView.findViewById(R.id.add_account_button);
77        mAddAccountButton.setOnClickListener(this);
78        mAddAccountButton.getBackground().setColorFilter(ContextCompat.getColor(getContext(), R
79                .color.primary_color), PorterDuff.Mode.SRC_ATOP);
80        mImportContactsButton = (Button) mView.findViewById(R.id.import_contacts_button);
81        mImportContactsButton.setOnClickListener(this);
82        mImportContactsButton.getBackground().setColorFilter(ContextCompat.getColor(getContext(),
83                R.color.primary_color), PorterDuff.Mode.SRC_ATOP);
84        mProgress = (ProgressBar) mView.findViewById(R.id.progress);
85
86        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
87            mButtonsContainer = mView.findViewById(R.id.buttons_container);
88        }
89
90        if (mProviderStatus != null) {
91            updateStatus(mProviderStatus);
92        }
93
94        return mView;
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                || providerStatus == ProviderStatusCompat.STATUS_CHANGING_LOCALE) {
107            updateViewsForBusyStatus();
108        }
109    }
110
111    /**
112     * Update views in the fragment when provider status is empty.
113     */
114    private void updateViewsForEmptyStatus() {
115        mMessageView.setVisibility(View.VISIBLE);
116        updateButtonVisibility(View.VISIBLE);
117        mProgress.setVisibility(View.GONE);
118    }
119
120    /**
121     * Update views in the fragment when provider status is busy.
122     */
123    private void updateViewsForBusyStatus() {
124        mMessageView.setVisibility(View.GONE);
125        mImageView.setVisibility(View.GONE);
126        updateButtonVisibility(View.GONE);
127        mProgress.setVisibility(View.VISIBLE);
128    }
129
130    @Override
131    public void onClick(View v) {
132        final int id = v.getId();
133        if (id == R.id.add_account_button) {
134            final Intent intent = ImplicitIntentsUtil.getIntentForAddingGoogleAccount();
135            ImplicitIntentsUtil.startActivityOutsideApp(getActivity(), intent);
136
137        } else if (id == R.id.import_contacts_button) {
138            ImportDialogFragment.show(getFragmentManager());
139
140        }
141    }
142
143    private void updateButtonVisibility(int visibility) {
144        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
145            mAddAccountButton.setVisibility(visibility);
146            mImportContactsButton.setVisibility(visibility);
147            mButtonsContainer.setVisibility(visibility);
148        } else {
149            mAddAccountButton.setVisibility(visibility);
150            mImportContactsButton.setVisibility(visibility);
151        }
152    }
153
154    @Override
155    public Context getContext() {
156        return getActivity();
157    }
158}
159