|
@@ -0,0 +1,159 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2013 The Android Open Kang Project
|
|
3
|
+ *
|
|
4
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+ * you may not use this file except in compliance with the License.
|
|
6
|
+ * You may obtain a copy of the License at
|
|
7
|
+ *
|
|
8
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+ *
|
|
10
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
11
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+ * See the License for the specific language governing permissions and
|
|
14
|
+ * limitations under the License.
|
|
15
|
+ */
|
|
16
|
+
|
|
17
|
+package com.aokp.romcontrol.fragments;
|
|
18
|
+
|
|
19
|
+import android.app.Activity;
|
|
20
|
+import android.app.Fragment;
|
|
21
|
+import android.content.ComponentName;
|
|
22
|
+import android.content.Context;
|
|
23
|
+import android.content.Intent;
|
|
24
|
+import android.content.pm.ApplicationInfo;
|
|
25
|
+import android.content.pm.PackageManager;
|
|
26
|
+import android.os.Bundle;
|
|
27
|
+import android.provider.Settings;
|
|
28
|
+import android.view.LayoutInflater;
|
|
29
|
+import android.view.Menu;
|
|
30
|
+import android.view.MenuInflater;
|
|
31
|
+import android.view.MenuItem;
|
|
32
|
+import android.view.View;
|
|
33
|
+import android.view.ViewGroup;
|
|
34
|
+import android.widget.ArrayAdapter;
|
|
35
|
+import android.widget.ListView;
|
|
36
|
+
|
|
37
|
+import com.aokp.romcontrol.R;
|
|
38
|
+import com.aokp.romcontrol.util.ShortcutPickerHelper;
|
|
39
|
+import com.google.android.apps.dashclock.ui.SwipeDismissListViewTouchListener;
|
|
40
|
+
|
|
41
|
+import java.util.ArrayList;
|
|
42
|
+
|
|
43
|
+public class AutoImmersiveSettingsFragment extends Fragment {
|
|
44
|
+
|
|
45
|
+ private Context mContext;
|
|
46
|
+
|
|
47
|
+ private ArrayList<String> appsPackageList = new ArrayList<String>();
|
|
48
|
+ private ArrayList<String> appsNameList = new ArrayList<String>();
|
|
49
|
+ private ArrayAdapter<String> mArrayAdapter;
|
|
50
|
+
|
|
51
|
+ public AutoImmersiveSettingsFragment() {
|
|
52
|
+
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ @Override
|
|
56
|
+ public void onCreate(Bundle savedInstanceState) {
|
|
57
|
+ super.onCreate(savedInstanceState);
|
|
58
|
+ setHasOptionsMenu(true);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ @Override
|
|
62
|
+ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
63
|
+ View v = inflater.inflate(R.layout.fragment_auto_immersive_settings, container, false);
|
|
64
|
+ mContext = getActivity();
|
|
65
|
+ appsPackageList = Settings.AOKP.getArrayList(mContext.getContentResolver(),
|
|
66
|
+ Settings.AOKP.KEY_AUTO_IMMERSIVE_ARRAY);
|
|
67
|
+ appsNameList = generateApplicationsNameList(appsPackageList);
|
|
68
|
+ ListView mAppsListView = (ListView) v.findViewById(R.id.listview_auto_immersive);
|
|
69
|
+ SwipeDismissListViewTouchListener touchListener =
|
|
70
|
+ new SwipeDismissListViewTouchListener(
|
|
71
|
+ mAppsListView,
|
|
72
|
+ new SwipeDismissListViewTouchListener.DismissCallbacks() {
|
|
73
|
+ @Override
|
|
74
|
+ public boolean canDismiss(int position) {
|
|
75
|
+ return true;
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ @Override
|
|
79
|
+ public void onDismiss(ListView listView, int[] reverseSortedPositions) {
|
|
80
|
+ for (int position : reverseSortedPositions) {
|
|
81
|
+ mArrayAdapter.remove(mArrayAdapter.getItem(position));
|
|
82
|
+ appsPackageList.remove(position);
|
|
83
|
+ }
|
|
84
|
+ mArrayAdapter.notifyDataSetChanged();
|
|
85
|
+ Settings.AOKP.putArrayList(mContext.getContentResolver(),
|
|
86
|
+ Settings.AOKP.KEY_AUTO_IMMERSIVE_ARRAY, appsPackageList);
|
|
87
|
+ }
|
|
88
|
+ });
|
|
89
|
+ mAppsListView.setOnTouchListener(touchListener);
|
|
90
|
+ mAppsListView.setOnScrollListener(touchListener.makeScrollListener());
|
|
91
|
+ mArrayAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, appsNameList);
|
|
92
|
+ mAppsListView.setAdapter(mArrayAdapter);
|
|
93
|
+ return v;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ @Override
|
|
97
|
+ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
98
|
+ super.onCreateOptionsMenu(menu, inflater);
|
|
99
|
+ inflater.inflate(R.menu.auto_immersive, menu);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ @Override
|
|
103
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
104
|
+ switch (item.getItemId()) {
|
|
105
|
+ case R.id.menu_add_app:
|
|
106
|
+ showAppChooserDialog();
|
|
107
|
+ return true;
|
|
108
|
+ default:
|
|
109
|
+ return false;
|
|
110
|
+ }
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ private void showAppChooserDialog() {
|
|
114
|
+ Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
|
|
115
|
+ mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
|
116
|
+
|
|
117
|
+ Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
|
|
118
|
+ pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
|
|
119
|
+ this.startActivityForResult(pickIntent, ShortcutPickerHelper.REQUEST_PICK_APPLICATION);
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
123
|
+
|
|
124
|
+ if (resultCode == Activity.RESULT_OK) {
|
|
125
|
+ switch (requestCode) {
|
|
126
|
+ case ShortcutPickerHelper.REQUEST_PICK_APPLICATION:
|
|
127
|
+ ComponentName componentName = data.getComponent();
|
|
128
|
+ final String packageName = componentName.getPackageName();
|
|
129
|
+ if (!appsPackageList.contains(packageName)) {
|
|
130
|
+ appsPackageList.add(packageName);
|
|
131
|
+ appsNameList.add(getApplicationName(mContext.getPackageManager(), packageName));
|
|
132
|
+ Settings.AOKP.putArrayList(mContext.getContentResolver(), Settings.AOKP.KEY_AUTO_IMMERSIVE_ARRAY, appsPackageList);
|
|
133
|
+ mArrayAdapter.notifyDataSetChanged();
|
|
134
|
+ break;
|
|
135
|
+ }
|
|
136
|
+ }
|
|
137
|
+ }
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ private ArrayList<String> generateApplicationsNameList (ArrayList<String> appsPackageList) {
|
|
141
|
+
|
|
142
|
+ final PackageManager pm = mContext.getPackageManager();
|
|
143
|
+ ArrayList<String> appsNames = new ArrayList<String>();
|
|
144
|
+ for (String mPackage : appsPackageList) {
|
|
145
|
+ appsNames.add(getApplicationName(pm, mPackage));
|
|
146
|
+ }
|
|
147
|
+ return appsNames;
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ private String getApplicationName(PackageManager pm, String packageName) {
|
|
151
|
+ ApplicationInfo ai;
|
|
152
|
+ try {
|
|
153
|
+ ai = pm.getApplicationInfo( packageName, 0);
|
|
154
|
+ } catch (PackageManager.NameNotFoundException e) {
|
|
155
|
+ ai = null;
|
|
156
|
+ }
|
|
157
|
+ return (String) (ai != null ? pm.getApplicationLabel(ai) : packageName);
|
|
158
|
+ }
|
|
159
|
+}
|