From 29cfd7e8352459f840444bc7c1d02ed4c6da0a26 Mon Sep 17 00:00:00 2001 From: Danesh M Date: Fri, 11 Sep 2015 16:37:37 -0700 Subject: 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 --- .../filemanager/ui/dialogs/AssociationsDialog.java | 33 +++--------- .../ui/widgets/CheckableRelativeLayout.java | 60 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 27 deletions(-) create mode 100644 src/com/cyanogenmod/filemanager/ui/widgets/CheckableRelativeLayout.java (limited to 'src/com') diff --git a/src/com/cyanogenmod/filemanager/ui/dialogs/AssociationsDialog.java b/src/com/cyanogenmod/filemanager/ui/dialogs/AssociationsDialog.java index 80d05c13..a90281dd 100644 --- a/src/com/cyanogenmod/filemanager/ui/dialogs/AssociationsDialog.java +++ b/src/com/cyanogenmod/filemanager/ui/dialogs/AssociationsDialog.java @@ -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()); } /** diff --git a/src/com/cyanogenmod/filemanager/ui/widgets/CheckableRelativeLayout.java b/src/com/cyanogenmod/filemanager/ui/widgets/CheckableRelativeLayout.java new file mode 100644 index 00000000..71f1c0ae --- /dev/null +++ b/src/com/cyanogenmod/filemanager/ui/widgets/CheckableRelativeLayout.java @@ -0,0 +1,60 @@ +/* + * 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; + } +} -- cgit v1.2.3