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

Commit 0f1d0187 authored by Danesh M's avatar Danesh M Committed by Ed Mancebo
Browse files

CMFileManager : Switch to checked states

Selected states are not being tracked properly when rotated.

Repro:

1. Click to open file
2. Tap on first item in resolver dialog
3. Rotate device
4. Press ok
5. Enjoy crash

issue-id: CYNGNOS-991

Change-Id: I9ef5d0571201957eac40edc8bc5e47700752dcc3
(cherry picked from commit 29cfd7e8)
parent 8fe99462
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -14,7 +14,8 @@
     limitations under the License.
 -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<com.cyanogenmod.filemanager.ui.widgets.CheckableRelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="@dimen/grid_width"
  android:layout_height="wrap_content"
  android:background="@drawable/material_selection"
@@ -43,4 +44,4 @@
      android:singleLine="true"
      android:ellipsize="end" />

</RelativeLayout>
 No newline at end of file
</com.cyanogenmod.filemanager.ui.widgets.CheckableRelativeLayout>
+6 −27
Original line number Diff line number Diff line
@@ -136,6 +136,7 @@ public class AssociationsDialog implements OnItemClickListener {
        this.mRemember.setVisibility(
                isPlatformSigned && this.mAllowPreferred ? View.VISIBLE : View.GONE);
        this.mGrid = (GridView)v.findViewById(R.id.associations_gridview);
        mGrid.setChoiceMode(GridView.CHOICE_MODE_SINGLE);
        AssociationsAdapter adapter =
                new AssociationsAdapter(this.mContext, this.mGrid, this.mIntents, this);
        this.mGrid.setAdapter(adapter);
@@ -209,12 +210,11 @@ public class AssociationsDialog implements OnItemClickListener {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // If the item is selected, then just open it like ActivityChooserView
        // If there is no parent, that means an internal call. In this case ignore it.
        if (parent != null && ((ViewGroup)view).isSelected()) {
        if (parent != null && mGrid.isItemChecked(position)) {
            this.mDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();

        } else {
            deselectAll();
            ((ViewGroup)view).setSelected(true);
            mGrid.setItemChecked(position, true);

            // Internal editors can be associated
            boolean isPlatformSigned = AndroidHelper.isAppPlatformSignature(this.mContext);
@@ -252,7 +252,7 @@ public class AssociationsDialog implements OnItemClickListener {
                        // Select the item
                        ViewGroup item = (ViewGroup)this.mGrid.getChildAt(i);
                        if (item != null) {
                            if (!item.isSelected()) {
                            if (!mGrid.isItemChecked(i)) {
                                onItemClick(null, item, i, item.getId());
                                this.mRemember.setChecked(true);
                                ret = false;
@@ -292,7 +292,7 @@ public class AssociationsDialog implements OnItemClickListener {
                ResolveInfo info = this.mIntents.get(i);
                if (info.activityInfo.name.equals(this.mPreferred.activityInfo.name)) {
                    ViewGroup item = (ViewGroup)this.mGrid.getChildAt(i);
                    if (item != null && item.isSelected()) {
                    if (item != null && mGrid.isItemChecked(i)) {
                        return true;
                    }
                }
@@ -301,19 +301,6 @@ public class AssociationsDialog implements OnItemClickListener {
        return false;
    }

    /**
     * Method that deselect all the items of the grid view
     */
    private void deselectAll() {
        int cc = this.mGrid.getChildCount();
        for (int i = 0; i < cc; i++) {
            ViewGroup item = (ViewGroup)this.mGrid.getChildAt(i);
            if (item != null) {
                item.setSelected(false);
            }
        }
    }

    /**
     * Method that returns the selected item of the grid view
     *
@@ -322,15 +309,7 @@ public class AssociationsDialog implements OnItemClickListener {
     */
    ResolveInfo getSelected() {
        AssociationsAdapter adapter = (AssociationsAdapter)this.mGrid.getAdapter();
        int cc = this.mGrid.getChildCount();
        int firstVisible = this.mGrid.getFirstVisiblePosition();
        for (int i = 0; i < cc; i++) {
            ViewGroup item = (ViewGroup)this.mGrid.getChildAt(i);
            if (item != null && item.isSelected()) {
                return adapter.getItem(i + firstVisible);
            }
        }
        return null;
        return adapter.getItem(mGrid.getCheckedItemPosition());
    }

    /**
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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.cyanogenmod.filemanager.ui.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.RelativeLayout;

public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
    private static final int[] CHECKED_STATE_SET = {
        android.R.attr.state_checked
    };
    private boolean mChecked;

    public CheckableRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {
        if (mChecked != checked) {
            mChecked = checked;
            refreshDrawableState();
        }
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (mChecked) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }
}