PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/omnirom/omniswitch/ui/CheckboxListDialog.java

https://gitlab.com/IllusionLP/packages_apps_OmniSwitch
Java | 279 lines | 231 code | 30 blank | 18 comment | 28 complexity | 8b0502b8237a2292a94e937cfd406644 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. * Copyright (C) 2013 The OmniROM Project
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. package org.omnirom.omniswitch.ui;
  19. import java.util.Arrays;
  20. import java.util.Iterator;
  21. import java.util.LinkedHashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.omnirom.omniswitch.R;
  25. import org.omnirom.omniswitch.dslv.DragSortController;
  26. import org.omnirom.omniswitch.dslv.DragSortListView;
  27. import android.app.AlertDialog;
  28. import android.content.Context;
  29. import android.content.DialogInterface;
  30. import android.graphics.Point;
  31. import android.graphics.drawable.Drawable;
  32. import android.os.Bundle;
  33. import android.view.LayoutInflater;
  34. import android.view.MotionEvent;
  35. import android.view.View;
  36. import android.view.ViewGroup;
  37. import android.widget.AdapterView;
  38. import android.widget.AdapterView.OnItemClickListener;
  39. import android.widget.ArrayAdapter;
  40. import android.widget.CheckBox;
  41. import android.widget.ImageView;
  42. import android.widget.TextView;
  43. public class CheckboxListDialog extends AlertDialog implements
  44. DialogInterface.OnClickListener {
  45. private String[] mListItems;
  46. private Drawable[] mListImages;
  47. private Map<Integer, Boolean> mCheckedItems;
  48. private DragSortListView mCheckboxListView;
  49. private LayoutInflater mInflater;
  50. private ArrayAdapter<String> mListAdapter;
  51. private ApplyRunnable mApplyRunnable;
  52. private String mTitle;
  53. ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
  54. ViewGroup.LayoutParams.MATCH_PARENT,
  55. ViewGroup.LayoutParams.WRAP_CONTENT);
  56. public interface ApplyRunnable {
  57. public void apply(Map<Integer, Boolean> buttons);
  58. };
  59. private class CheckboxListAdapter extends ArrayAdapter<String> {
  60. public CheckboxListAdapter(Context context, int resource, List<String> values) {
  61. super(context, R.layout.checkbox_item, resource, values);
  62. }
  63. @Override
  64. public View getView(int position, View convertView, ViewGroup parent) {
  65. View rowView = mInflater.inflate(R.layout.checkbox_item, parent, false);
  66. final TextView item = (TextView)rowView.findViewById(R.id.item_text);
  67. int orderPosition = getPositionOfItem(position, mCheckedItems);
  68. item.setText(mListItems[orderPosition]);
  69. final CheckBox check = (CheckBox)rowView.findViewById(R.id.item_check);
  70. check.setChecked(mCheckedItems.get(orderPosition));
  71. final ImageView image = (ImageView)rowView.findViewById(R.id.item_image);
  72. image.setImageDrawable(mListImages[orderPosition]);
  73. return rowView;
  74. }
  75. }
  76. private int getPositionOfItem(int position, Map<Integer, Boolean> buttons) {
  77. int i = 0;
  78. Iterator<Integer> nextKey = buttons.keySet().iterator();
  79. while(nextKey.hasNext()){
  80. Integer key = nextKey.next();
  81. if (i == position){
  82. return key;
  83. }
  84. i++;
  85. }
  86. return 0;
  87. }
  88. private void setValueAtPosition(int position, Map<Integer, Boolean> buttons, boolean value) {
  89. int i = 0;
  90. Iterator<Integer> nextKey = buttons.keySet().iterator();
  91. while(nextKey.hasNext()){
  92. Integer key = nextKey.next();
  93. if (i == position){
  94. buttons.put(key, value);
  95. break;
  96. }
  97. i++;
  98. }
  99. }
  100. private Boolean getValueAtPosition(int position, Map<Integer, Boolean> buttons) {
  101. int i = 0;
  102. Iterator<Integer> nextKey = buttons.keySet().iterator();
  103. while(nextKey.hasNext()){
  104. Integer key = nextKey.next();
  105. if (i == position){
  106. return buttons.get(key);
  107. }
  108. i++;
  109. }
  110. return null;
  111. }
  112. private class CheckboxListDragSortController extends DragSortController {
  113. public CheckboxListDragSortController() {
  114. super(mCheckboxListView, R.id.drag_handle,
  115. DragSortController.ON_DOWN,
  116. DragSortController.FLING_RIGHT_REMOVE);
  117. setRemoveEnabled(false);
  118. setSortEnabled(true);
  119. setBackgroundColor(0x363636);
  120. }
  121. @Override
  122. public void onDragFloatView(View floatView, Point floatPoint,
  123. Point touchPoint) {
  124. floatView.setLayoutParams(params);
  125. mCheckboxListView.setFloatAlpha(0.8f);
  126. }
  127. @Override
  128. public View onCreateFloatView(int position) {
  129. View v = mListAdapter.getView(position, null,
  130. mCheckboxListView);
  131. v.setLayoutParams(params);
  132. return v;
  133. }
  134. @Override
  135. public void onDestroyFloatView(View floatView) {
  136. }
  137. }
  138. public CheckboxListDialog(Context context, String[] items, Drawable[] images, Map<Integer, Boolean> checked, ApplyRunnable applyRunnable, String title) {
  139. super(context);
  140. mTitle = title;
  141. mApplyRunnable = applyRunnable;
  142. mListItems = items;
  143. mListImages = images;
  144. mCheckedItems = checked;
  145. mInflater = (LayoutInflater) context
  146. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  147. }
  148. private void applyChanges() {
  149. mApplyRunnable.apply(mCheckedItems);
  150. }
  151. @Override
  152. public void onClick(DialogInterface dialog, int which) {
  153. if (which == DialogInterface.BUTTON_POSITIVE) {
  154. applyChanges();
  155. } else if (which == DialogInterface.BUTTON_NEGATIVE) {
  156. cancel();
  157. }
  158. }
  159. @Override
  160. protected void onCreate(Bundle savedInstanceState) {
  161. final Context context = getContext();
  162. final View view = getLayoutInflater().inflate(
  163. R.layout.checkbox_list, null);
  164. setView(view);
  165. setTitle(mTitle);
  166. setCancelable(true);
  167. setButton(DialogInterface.BUTTON_POSITIVE,
  168. context.getString(android.R.string.ok), this);
  169. setButton(DialogInterface.BUTTON_NEGATIVE,
  170. context.getString(android.R.string.cancel), this);
  171. super.onCreate(savedInstanceState);
  172. mCheckboxListView = (DragSortListView) view.findViewById(R.id.item_list);
  173. mListAdapter = new CheckboxListAdapter(getContext(),
  174. android.R.layout.simple_list_item_multiple_choice, Arrays.asList(mListItems));
  175. mCheckboxListView.setAdapter(mListAdapter);
  176. mCheckboxListView.setOnItemClickListener(new OnItemClickListener() {
  177. @Override
  178. public void onItemClick(AdapterView<?> parent, View view,
  179. int position, long id) {
  180. Boolean value = getValueAtPosition(position, mCheckedItems);
  181. if (value != null){
  182. setValueAtPosition(position, mCheckedItems, !value);
  183. mListAdapter.notifyDataSetChanged();
  184. }
  185. }
  186. });
  187. final DragSortController dragSortController = new CheckboxListDragSortController();
  188. mCheckboxListView.setFloatViewManager(dragSortController);
  189. mCheckboxListView
  190. .setDropListener(new DragSortListView.DropListener() {
  191. @Override
  192. public void drop(int from, int to) {
  193. Map<Integer, Boolean> newItems = new LinkedHashMap<Integer, Boolean>();
  194. Iterator<Integer> nextKey = mCheckedItems.keySet().iterator();
  195. Integer fromKey = null;
  196. Boolean fromValue = null;
  197. int i = 0;
  198. while(nextKey.hasNext()){
  199. Integer key = nextKey.next();
  200. Boolean value = mCheckedItems.get(key);
  201. if (i == from){
  202. fromKey = key;
  203. fromValue = value;
  204. break;
  205. }
  206. i++;
  207. }
  208. if (fromKey != null && fromValue != null){
  209. nextKey = mCheckedItems.keySet().iterator();
  210. i = 0;
  211. boolean added = false;
  212. while(nextKey.hasNext()){
  213. Integer key = nextKey.next();
  214. Boolean value = mCheckedItems.get(key);
  215. if (i == to && to != mCheckedItems.size() - 1){
  216. if (to > from){
  217. newItems.put(key, value);
  218. newItems.put(fromKey, fromValue);
  219. added = true;
  220. i++;
  221. continue;
  222. }
  223. newItems.put(fromKey, fromValue);
  224. added = true;
  225. } else if (i == from){
  226. i++;
  227. continue;
  228. }
  229. newItems.put(key, value);
  230. i++;
  231. }
  232. // added at the end
  233. if (!added){
  234. newItems.put(fromKey, fromValue);
  235. }
  236. mCheckedItems.clear();
  237. mCheckedItems.putAll(newItems);
  238. mListAdapter.notifyDataSetChanged();
  239. }
  240. }
  241. });
  242. mCheckboxListView.setOnTouchListener(new View.OnTouchListener() {
  243. @Override
  244. public boolean onTouch(View view, MotionEvent motionEvent) {
  245. return dragSortController.onTouch(view, motionEvent);
  246. }
  247. });
  248. }
  249. }