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

Commit 7b5e52bf authored by Mihai Nita's avatar Mihai Nita Committed by android-build-merger
Browse files

Merge "Fix checkboxes and warning dialog lost when device rotates" into nyc-dev

am: 50669fa3

* commit '50669fa3':
  Fix checkboxes and warning dialog lost when device rotates

Change-Id: Id4f7d65e987f1c340898d1c1136260b0b1d72c4f
parents 4012ca71 50669fa3
Loading
Loading
Loading
Loading
+41 −2
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.localepicker;


import android.content.Context;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.support.v7.widget.helper.ItemTouchHelper;
@@ -36,6 +37,7 @@ import com.android.internal.app.LocaleStore;
import com.android.settings.R;
import com.android.settings.R;


import java.text.NumberFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
import java.util.Locale;
import java.util.Locale;
@@ -45,6 +47,7 @@ class LocaleDragAndDropAdapter
        extends RecyclerView.Adapter<LocaleDragAndDropAdapter.CustomViewHolder> {
        extends RecyclerView.Adapter<LocaleDragAndDropAdapter.CustomViewHolder> {


    private static final String TAG = "LocaleDragAndDropAdapter";
    private static final String TAG = "LocaleDragAndDropAdapter";
    private static final String CFGKEY_SELECTED_LOCALES = "selectedLocales";
    private final Context mContext;
    private final Context mContext;
    private final List<LocaleStore.LocaleInfo> mFeedItemList;
    private final List<LocaleStore.LocaleInfo> mFeedItemList;
    private final ItemTouchHelper mItemTouchHelper;
    private final ItemTouchHelper mItemTouchHelper;
@@ -105,6 +108,7 @@ class LocaleDragAndDropAdapter
            private static final int SELECTION_LOST = 0;
            private static final int SELECTION_LOST = 0;
            private static final int SELECTION_UNCHANGED = -1;
            private static final int SELECTION_UNCHANGED = -1;
            private int mSelectionStatus = SELECTION_UNCHANGED;
            private int mSelectionStatus = SELECTION_UNCHANGED;

            @Override
            @Override
            public void onChildDraw(Canvas c, RecyclerView recyclerView,
            public void onChildDraw(Canvas c, RecyclerView recyclerView,
                    RecyclerView.ViewHolder viewHolder, float dX, float dY,
                    RecyclerView.ViewHolder viewHolder, float dX, float dY,
@@ -148,7 +152,6 @@ class LocaleDragAndDropAdapter
    public void onBindViewHolder(final CustomViewHolder holder, int i) {
    public void onBindViewHolder(final CustomViewHolder holder, int i) {
        final LocaleStore.LocaleInfo feedItem = mFeedItemList.get(i);
        final LocaleStore.LocaleInfo feedItem = mFeedItemList.get(i);
        final LocaleDragCell dragCell = holder.getLocaleDragCell();
        final LocaleDragCell dragCell = holder.getLocaleDragCell();

        String label = feedItem.getFullNameNative();
        String label = feedItem.getFullNameNative();
        dragCell.setLabel(label);
        dragCell.setLabel(label);
        dragCell.setLocalized(feedItem.isTranslated());
        dragCell.setLocalized(feedItem.isTranslated());
@@ -156,7 +159,7 @@ class LocaleDragAndDropAdapter
        dragCell.setShowCheckbox(mRemoveMode);
        dragCell.setShowCheckbox(mRemoveMode);
        dragCell.setShowMiniLabel(!mRemoveMode);
        dragCell.setShowMiniLabel(!mRemoveMode);
        dragCell.setShowHandle(!mRemoveMode && mDragEnabled);
        dragCell.setShowHandle(!mRemoveMode && mDragEnabled);
        dragCell.setChecked(false);
        dragCell.setChecked(mRemoveMode ? feedItem.getChecked() : false);
        dragCell.setTag(feedItem);
        dragCell.setTag(feedItem);
        dragCell.getCheckbox()
        dragCell.getCheckbox()
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@@ -286,4 +289,40 @@ class LocaleDragAndDropAdapter
    private void setDragEnabled(boolean enabled) {
    private void setDragEnabled(boolean enabled) {
        mDragEnabled = enabled;
        mDragEnabled = enabled;
    }
    }

    /**
     * Saves the list of checked locales to preserve status when the list is destroyed.
     * (for instance when the device is rotated)
     * @param outInstanceState Bundle in which to place the saved state
     */
    public void saveState(Bundle outInstanceState) {
        if (outInstanceState != null) {
            final ArrayList<String> selectedLocales = new ArrayList<>();
            for (LocaleStore.LocaleInfo li : mFeedItemList) {
                if (li.getChecked()) {
                    selectedLocales.add(li.getId());
                }
            }
            outInstanceState.putStringArrayList(CFGKEY_SELECTED_LOCALES, selectedLocales);
        }
    }

    /**
     * Restores the list of checked locales to preserve status when the list is recreated.
     * (for instance when the device is rotated)
     * @param savedInstanceState Bundle with the data saved by {@link #saveState(Bundle)}
     */
    public void restoreState(Bundle savedInstanceState) {
        if (savedInstanceState != null && mRemoveMode) {
            final ArrayList<String> selectedLocales =
                    savedInstanceState.getStringArrayList(CFGKEY_SELECTED_LOCALES);
            if (selectedLocales == null || selectedLocales.isEmpty()) {
                return;
            }
            for (LocaleStore.LocaleInfo li : mFeedItemList) {
                li.setChecked(selectedLocales.contains(li.getId()));
            }
            notifyItemRangeChanged(0, mFeedItemList.size());
        }
    }
}
}
+81 −14
Original line number Original line Diff line number Diff line
@@ -31,6 +31,7 @@ import android.view.MenuItem;
import android.view.View;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout;

import com.android.internal.app.LocalePicker;
import com.android.internal.app.LocalePicker;
import com.android.internal.app.LocalePickerWithRegion;
import com.android.internal.app.LocalePickerWithRegion;
import com.android.internal.app.LocaleStore;
import com.android.internal.app.LocaleStore;
@@ -48,12 +49,15 @@ import java.util.Locale;
public class LocaleListEditor extends SettingsPreferenceFragment
public class LocaleListEditor extends SettingsPreferenceFragment
        implements LocalePickerWithRegion.LocaleSelectedListener {
        implements LocalePickerWithRegion.LocaleSelectedListener {


    private static final String CFGKEY_REMOVE_MODE = "localeRemoveMode";
    private static final String CFGKEY_REMOVE_DIALOG = "showingLocaleRemoveDialog";
    private static final int MENU_ID_REMOVE = Menu.FIRST + 1;
    private static final int MENU_ID_REMOVE = Menu.FIRST + 1;


    private LocaleDragAndDropAdapter mAdapter;
    private LocaleDragAndDropAdapter mAdapter;
    private Menu mMenu;
    private Menu mMenu;
    private boolean mRemoveMode;
    private View mAddLanguage;
    private View mAddLanguage;
    private boolean mRemoveMode;
    private boolean mShowingRemoveDialog;


    @Override
    @Override
    protected int getMetricsCategory() {
    protected int getMetricsCategory() {
@@ -82,15 +86,45 @@ public class LocaleListEditor extends SettingsPreferenceFragment
        return result;
        return result;
    }
    }


    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
        if (savedInstanceState != null) {
            mRemoveMode = savedInstanceState.getBoolean(CFGKEY_REMOVE_MODE, false);
            mShowingRemoveDialog = savedInstanceState.getBoolean(CFGKEY_REMOVE_DIALOG, false);
        }
        setRemoveMode(mRemoveMode);
        mAdapter.restoreState(savedInstanceState);

        if (mShowingRemoveDialog) {
            showRemoveLocaleWarningDialog();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(CFGKEY_REMOVE_MODE, mRemoveMode);
        outState.putBoolean(CFGKEY_REMOVE_DIALOG, mShowingRemoveDialog);
        mAdapter.saveState(outState);
    }

    @Override
    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == MENU_ID_REMOVE) {
        switch (menuItem.getItemId()) {
            case MENU_ID_REMOVE:
                if (mRemoveMode) {
                if (mRemoveMode) {
                removeLocaleWarningDialog();
                    showRemoveLocaleWarningDialog();
                } else {
                } else {
                    setRemoveMode(true);
                    setRemoveMode(true);
                }
                }
                return true;
                return true;
            case android.R.id.home:
                if (mRemoveMode) {
                    setRemoveMode(false);
                    return true;
                }
                break;
        }
        }
        return super.onOptionsItemSelected(menuItem);
        return super.onOptionsItemSelected(menuItem);
    }
    }
@@ -98,12 +132,15 @@ public class LocaleListEditor extends SettingsPreferenceFragment
    private void setRemoveMode(boolean mRemoveMode) {
    private void setRemoveMode(boolean mRemoveMode) {
        this.mRemoveMode = mRemoveMode;
        this.mRemoveMode = mRemoveMode;
        mAdapter.setRemoveMode(mRemoveMode);
        mAdapter.setRemoveMode(mRemoveMode);
        mMenu.findItem(MENU_ID_REMOVE).setShowAsAction(
                mRemoveMode ? MenuItem.SHOW_AS_ACTION_ALWAYS : MenuItem.SHOW_AS_ACTION_NEVER);
        mAddLanguage.setVisibility(mRemoveMode ? View.INVISIBLE : View.VISIBLE);
        mAddLanguage.setVisibility(mRemoveMode ? View.INVISIBLE : View.VISIBLE);
        updateVisibilityOfRemoveMenu();
    }
    }


    private void removeLocaleWarningDialog() {
    // Show the appropriate warning when the user tries to remove locales.
    // Shows no warning if there is no locale checked, shows a warning
    // about removing all the locales if all of them are checked, and
    // a "regular" warning otherwise.
    private void showRemoveLocaleWarningDialog() {
        int checkedCount = mAdapter.getCheckedCount();
        int checkedCount = mAdapter.getCheckedCount();


        // Nothing checked, just exit remove mode without a warning dialog
        // Nothing checked, just exit remove mode without a warning dialog
@@ -114,6 +151,7 @@ public class LocaleListEditor extends SettingsPreferenceFragment


        // All locales selected, warning dialog, can't remove them all
        // All locales selected, warning dialog, can't remove them all
        if (checkedCount == mAdapter.getItemCount()) {
        if (checkedCount == mAdapter.getItemCount()) {
            mShowingRemoveDialog = true;
            new AlertDialog.Builder(getActivity())
            new AlertDialog.Builder(getActivity())
                    .setTitle(R.string.dlg_remove_locales_error_title)
                    .setTitle(R.string.dlg_remove_locales_error_title)
                    .setMessage(R.string.dlg_remove_locales_error_message)
                    .setMessage(R.string.dlg_remove_locales_error_message)
@@ -122,6 +160,12 @@ public class LocaleListEditor extends SettingsPreferenceFragment
                        public void onClick(DialogInterface dialog, int which) {
                        public void onClick(DialogInterface dialog, int which) {
                        }
                        }
                    })
                    })
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            mShowingRemoveDialog = false;
                        }
                    })
                    .create()
                    .create()
                    .show();
                    .show();
            return;
            return;
@@ -129,21 +173,38 @@ public class LocaleListEditor extends SettingsPreferenceFragment


        final String title = getResources().getQuantityString(R.plurals.dlg_remove_locales_title,
        final String title = getResources().getQuantityString(R.plurals.dlg_remove_locales_title,
                checkedCount);
                checkedCount);
        mShowingRemoveDialog = true;
        new AlertDialog.Builder(getActivity())
        new AlertDialog.Builder(getActivity())
                .setTitle(title)
                .setTitle(title)
                .setMessage(R.string.dlg_remove_locales_message)
                .setMessage(R.string.dlg_remove_locales_message)
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                    @Override
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    public void onClick(DialogInterface dialog, int which) {
                        setRemoveMode(!mRemoveMode);
                        setRemoveMode(false);
                    }
                    }
                })
                })
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    public void onClick(DialogInterface dialog, int which) {
                        // This is a sensitive area to change.
                        // removeChecked() triggers a system update and "kills" the frame.
                        // This means that saveState + restoreState are called before
                        // setRemoveMode is called.
                        // So we want that mRemoveMode and dialog status have the right values
                        // before that save.
                        // We can't just call setRemoveMode(false) before calling removeCheched
                        // because that unchecks all items and removeChecked would have nothing
                        // to remove.
                        mRemoveMode = false;
                        mShowingRemoveDialog = false;
                        mAdapter.removeChecked();
                        mAdapter.removeChecked();
                        setRemoveMode(!mRemoveMode);
                        setRemoveMode(false);
                        updateVisibilityOfRemoveMenu();
                    }
                })
                .setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        mShowingRemoveDialog = false;
                    }
                    }
                })
                })
                .create()
                .create()
@@ -208,8 +269,14 @@ public class LocaleListEditor extends SettingsPreferenceFragment
    // Hide the "Remove" menu if there is only one locale in the list, show it otherwise
    // Hide the "Remove" menu if there is only one locale in the list, show it otherwise
    // This is called when the menu is first created, and then one add / remove locale
    // This is called when the menu is first created, and then one add / remove locale
    private void updateVisibilityOfRemoveMenu() {
    private void updateVisibilityOfRemoveMenu() {
        if (mMenu == null) {
            return;
        }

        final MenuItem menuItemRemove = mMenu.findItem(MENU_ID_REMOVE);
        final MenuItem menuItemRemove = mMenu.findItem(MENU_ID_REMOVE);
        if (menuItemRemove != null) {
        if (menuItemRemove != null) {
            menuItemRemove.setShowAsAction(
                    mRemoveMode ? MenuItem.SHOW_AS_ACTION_ALWAYS : MenuItem.SHOW_AS_ACTION_NEVER);
            menuItemRemove.setVisible(mAdapter.getItemCount() > 1);
            menuItemRemove.setVisible(mAdapter.getItemCount() > 1);
        }
        }
    }
    }