Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 491cca57 authored by yaolu's avatar yaolu
Browse files

Show sync-off alert

In all contacts list:
alert user when global sync is off if user never dismissed before,
until sync setting is toggled on then off again.

In account view:
when global sync is off, show alert with the same logic for all
contacts list;
when global sync is on, alert user when contacts sync for this account
is off if user never dismissed before, until contacts sync is toggled
on then off again.

Bug: 31755559

Test: Manually tested the scenarios:
1) Add/remove account,
2) switch between all contacts list, account view, and label views,
3) update contacts information,
3) turn on sync/dismiss from alert,
when global sync is on/off, contacts syncs are all on/off, contacts
sync is on/off for a particular account.

Change-Id: Id5001847270d5a984942c63a42edb5f9401bc2c2
parent 87e14288
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
@@ -28,6 +28,37 @@
    android:visibility="gone"
    android:background="?attr/contact_browser_background" >

    <LinearLayout
        android:id="@+id/alert_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@color/alert_background"
        android:paddingStart="20dp"
        android:visibility="gone">

        <TextView
            android:id="@+id/alert_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:layout_weight="1"
            android:textColor="@android:color/black"
            android:textSize="16sp"/>

        <ImageView
            android:id="@+id/alert_dismiss_icon"
            android:layout_width="56dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:contentDescription="@string/dismiss_sync_alert"
            android:background="?android:attr/selectableItemBackground"
            android:scaleType="center"
            android:src="@drawable/ic_cancel_black_24dp" />
    </LinearLayout>

    <!-- Shown only when an Account filter is set.
         - paddingTop should be here to show "shade" effect correctly. -->
    <include layout="@layout/account_filter_header" />
+3 −0
Original line number Diff line number Diff line
@@ -260,4 +260,7 @@

    <!-- tint color for device account icons -->
    <color name="device_account_tint_color">#7f7f7f</color>

    <!-- Background color for sync-off alert. -->
    <color name="alert_background">#e0e0e0</color>
</resources>
+20 −0
Original line number Diff line number Diff line
@@ -1819,4 +1819,24 @@
    <!-- Content description of the cancel navigation icon shown in SIM import dialog toolbar -->
    <string name="sim_import_cancel_content_description">Cancel import</string>

    <!-- Alert for letting user know that their device auto-sync setting is turned off,
         in case they are wondering why they are not seeing any contact. [CHAR LIMIT=150] -->
    <string name="auto_sync_off">Auto-sync is off. Tap to turn on.</string>

    <!-- Content description for the "X" image icon for dismissing an alert.[CHAR LIMIT=50] -->
    <string name="dismiss_sync_alert">Dismiss</string>

    <!-- Alert for letting user know that their account level sync setting is turned off,
         in case they are wondering why they are not seeing any contact. [CHAR LIMIT=150] -->
    <string name="account_sync_off">Account sync is off. Tap to turn on.</string>

    <!-- Title of dialog to turn auto-sync on [CHAR LIMIT=100] -->
    <string name="turn_auto_sync_on_dialog_title">Turn auto-sync on?</string>

    <!-- Text of dialog to turn auto-sync on [CHAR LIMIT=500] -->
    <string name="turn_auto_sync_on_dialog_body">Changes you make to all apps and accounts,
        not just Contacts, will be synchronized between the web and your devices.</string>

    <!-- Confirm button text for dialog to turn auto-sync on [CHAR LIMIT=30] -->
    <string name="turn_auto_sync_on_dialog_confirm_btn">Turn on</string>
</resources>
+105 −1
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.content.Intent;
import android.content.Loader;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Rect;
import android.net.Uri;
@@ -88,7 +89,8 @@ import java.util.Locale;
 * Fragment containing a contact list used for browsing (as compared to
 * picking a contact with one of the PICK intents).
 */
public class DefaultContactBrowseListFragment extends ContactBrowseListFragment {
public class DefaultContactBrowseListFragment extends ContactBrowseListFragment
        implements EnableGlobalSyncDialogFragment.Listener {

    private static final String TAG = "DefaultListFragment";
    private static final String ENABLE_DEBUG_OPTIONS_HIDDEN_CODE = "debug debug!";
@@ -104,6 +106,11 @@ public class DefaultContactBrowseListFragment extends ContactBrowseListFragment
    private TextView mSearchProgressText;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private View mAlertContainer;
    private TextView mAlertText;
    private ImageView mAlertDismissIcon;
    private int mReasonSyncOff = SyncUtil.SYNC_SETTING_SYNC_ON;

    private boolean mContactsAvailable;
    private boolean mEnableDebugMenuOptions;
    private boolean mIsRecreatedInstance;
@@ -164,6 +171,10 @@ public class DefaultContactBrowseListFragment extends ContactBrowseListFragment
                    maybeHideCheckBoxes();
                    mActivity.invalidateOptionsMenu();
                    mActivity.showFabWithAnimation(/* showFab */ true);

                    // Alert user if sync is off and not dismissed before
                    setSyncOffAlert();

                    // Determine whether the account has pullToRefresh feature
                    if (Flags.getInstance(getContext()).getBoolean(Experiments.PULL_TO_REFRESH)) {
                        setSwipeRefreshLayoutEnabledOrNot(getFilter());
@@ -452,6 +463,66 @@ public class DefaultContactBrowseListFragment extends ContactBrowseListFragment

        mSearchProgress = getView().findViewById(R.id.search_progress);
        mSearchProgressText = (TextView) mSearchHeaderView.findViewById(R.id.totalContactsText);

        mAlertContainer = getView().findViewById(R.id.alert_container);
        mAlertText = (TextView) mAlertContainer.findViewById(R.id.alert_text);
        mAlertDismissIcon = (ImageView) mAlertContainer.findViewById(R.id.alert_dismiss_icon);
        mAlertText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                turnSyncOn();
            }
        });
        mAlertDismissIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mAlertContainer.setVisibility(View.GONE);
    }

    private void turnSyncOn() {
        final ContactListFilter filter = getFilter();
        if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT
                && mReasonSyncOff == SyncUtil.SYNC_SETTING_ACCOUNT_SYNC_OFF) {
            ContentResolver.setSyncAutomatically(
                    new Account(filter.accountName, filter.accountType),
                    ContactsContract.AUTHORITY, true);
            mAlertContainer.setVisibility(View.GONE);
        } else {
            final EnableGlobalSyncDialogFragment dialog = new
                    EnableGlobalSyncDialogFragment();
            dialog.show(this, filter);
        }
    }

    @Override
    public void onEnableAutoSync(ContactListFilter filter) {
        // Turn on auto-sync
        ContentResolver.setMasterSyncAutomatically(true);
        // Also enable Contacts sync
        final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(
                getContext()).getAccounts(/* contactsWritableOnly */ true);
        final List<Account> syncableAccounts = filter.getSyncableAccounts(accounts);
        if (syncableAccounts != null && syncableAccounts.size() > 0) {
            for (Account account : syncableAccounts) {
                ContentResolver.setSyncAutomatically(new Account(account.name, account.type),
                        ContactsContract.AUTHORITY, true);
            }
        }
        mAlertContainer.setVisibility(View.GONE);
    }

    private void dismiss() {
        if (mReasonSyncOff == SyncUtil.SYNC_SETTING_GLOBAL_SYNC_OFF) {
            SharedPreferenceUtil.incNumOfDismissesForAutoSyncOff(getContext());
        } else if (mReasonSyncOff == SyncUtil.SYNC_SETTING_ACCOUNT_SYNC_OFF) {
            SharedPreferenceUtil.incNumOfDismissesForAccountSyncOff(
                    getContext(), getFilter().accountName);
        }
        mAlertContainer.setVisibility(View.GONE);
    }

    private void initSwipeRefreshLayout() {
@@ -503,6 +574,36 @@ public class DefaultContactBrowseListFragment extends ContactBrowseListFragment
        }
    }

    private void setSyncOffAlert() {
        final ContactListFilter filter = getFilter();
        final Account account =  filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT
                && filter.isGoogleAccountType()
                ? new Account(filter.accountName, filter.accountType) : null;

        if (account == null && !filter.isContactsFilterType()) {
            mAlertContainer.setVisibility(View.GONE);
        } else {
            mReasonSyncOff = SyncUtil.calculateReasonSyncOff(getContext(), account);
            final boolean isAlertVisible =
                    SyncUtil.isAlertVisible(getContext(), account, mReasonSyncOff);
            setSyncOffMsg(mReasonSyncOff);
            mAlertContainer.setVisibility(isAlertVisible ? View.VISIBLE : View.GONE);
        }
    }

    private void setSyncOffMsg(int reason) {
        final Resources resources = getResources();
        switch (reason) {
            case SyncUtil.SYNC_SETTING_GLOBAL_SYNC_OFF:
                mAlertText.setText(resources.getString(R.string.auto_sync_off));
                break;
            case SyncUtil.SYNC_SETTING_ACCOUNT_SYNC_OFF:
                mAlertText.setText(resources.getString(R.string.account_sync_off));
                break;
            default:
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
@@ -702,6 +803,9 @@ public class DefaultContactBrowseListFragment extends ContactBrowseListFragment
        updateListFilter(filter, restoreSelectedUri);
        mActivity.setTitle(AccountFilterUtil.getActionBarTitleForFilter(mActivity, filter));

        // Alert user if sync is off and not dismissed before
        setSyncOffAlert();

        // Determine whether the account has pullToRefresh feature
        if (Flags.getInstance(getContext()).getBoolean(Experiments.PULL_TO_REFRESH)) {
            setSwipeRefreshLayoutEnabledOrNot(filter);
+85 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.list;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;

import com.android.contacts.R;
import com.android.contacts.common.list.ContactListFilter;

/**
 * Confirmation dialog for turning global auto-sync setting on.
 */
public class EnableGlobalSyncDialogFragment extends DialogFragment{

    private static final String ARG_FILTER = "filter";
    private ContactListFilter mFilter;

    /**
     * Callbacks for the dialog host.
     */
    public interface Listener {

        /**
         * Invoked after the user has confirmed that they want to turn on sync.
         *
         * @param filter the filter of current contacts list.
         */
        void onEnableAutoSync(ContactListFilter filter);
    }

    public static void show(DefaultContactBrowseListFragment fragment, ContactListFilter filter) {
        final Bundle args = new Bundle();
        args.putParcelable(ARG_FILTER, filter);

        final EnableGlobalSyncDialogFragment dialog = new
                EnableGlobalSyncDialogFragment();
        dialog.setTargetFragment(fragment, 0);
        dialog.setArguments(args);
        dialog.show(fragment.getFragmentManager(), "globalSync");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFilter = getArguments().getParcelable(ARG_FILTER);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Listener targetListener = (Listener) getTargetFragment();
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.turn_auto_sync_on_dialog_title)
                .setMessage(R.string.turn_auto_sync_on_dialog_body)
                .setPositiveButton(R.string.turn_auto_sync_on_dialog_confirm_btn,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                    if (targetListener != null) {
                                        targetListener.onEnableAutoSync(mFilter);
                                    }
                                }
                            });
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.setCancelable(false);
        return builder.create();
    }
}
Loading