1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* Copyright (C) 2013 The CyanogenMod 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.android.settings.cyanogenmod;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import static com.android.internal.util.cm.NavigationRingConstants.*;
import com.android.internal.util.cm.NavigationRingHelpers;
import com.android.internal.widget.multiwaveview.GlowPadView;
import com.android.internal.widget.multiwaveview.TargetDrawable;
import com.android.settings.R;
import com.android.settings.Utils;
import java.util.ArrayList;
public class NavRing extends Fragment implements
ShortcutPickHelper.OnPickListener, GlowPadView.OnTriggerListener {
private GlowPadView mGlowPadView;
private ShortcutPickHelper mPicker;
private String[] mTargetActivities;
private ViewGroup mContainer;
private int mTargetIndex = 0;
private int mStartPosOffset;
private int mEndPosOffset;
private boolean mIsLandscape;
private boolean mIsScreenLarge;
private ActionHolder mActions;
private static final int MENU_RESET = Menu.FIRST;
private static final int MENU_SAVE = Menu.FIRST + 1;
private class ActionHolder {
private ArrayList<CharSequence> mAvailableEntries = new ArrayList<CharSequence>();
private ArrayList<String> mAvailableValues = new ArrayList<String>();
public void addAction(String action, int entryResId) {
mAvailableEntries.add(getString(entryResId));
mAvailableValues.add(action);
}
public int getActionIndex(String action) {
int count = mAvailableValues.size();
for (int i = 0; i < count; i++) {
if (TextUtils.equals(mAvailableValues.get(i), action)) {
return i;
}
}
return -1;
}
public String getAction(int index) {
if (index > mAvailableValues.size()) {
return null;
}
return mAvailableValues.get(index);
}
public CharSequence[] getEntries() {
return mAvailableEntries.toArray(new CharSequence[mAvailableEntries.size()]);
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContainer = container;
setHasOptionsMenu(true);
createActionList();
mIsScreenLarge = !Utils.isPhone(getActivity());
mIsLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
mPicker = new ShortcutPickHelper(getActivity(), this);
return inflater.inflate(R.layout.navigation_ring_targets, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mGlowPadView = ((GlowPadView) getActivity().findViewById(R.id.navring_target));
mGlowPadView.setOnTriggerListener(this);
updateDrawables();
}
private void createActionList() {
mActions = new ActionHolder();
mActions.addAction(ACTION_NONE, R.string.navring_action_none);
if (NavigationRingHelpers.isAssistantAvailable(getActivity())) {
mActions.addAction(ACTION_ASSIST, R.string.navring_action_google_now);
}
if (NavigationRingHelpers.isTorchAvailable(getActivity())) {
mActions.addAction(ACTION_TORCH, R.string.navring_action_torch);
}
mActions.addAction(ACTION_SCREENSHOT, R.string.navring_action_take_screenshot);
mActions.addAction(ACTION_IME, R.string.navring_action_open_ime_switcher);
mActions.addAction(ACTION_SILENT, R.string.navring_action_ring_silent);
Vibrator vibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null && vibrator.hasVibrator()) {
mActions.addAction(ACTION_VIBRATE, R.string.navring_action_ring_vibrate);
mActions.addAction(ACTION_RING_SILENT_VIBRATE, R.string.navring_action_ring_vibrate_silent);
}
mActions.addAction(ACTION_KILL, R.string.navring_action_kill_app);
mActions.addAction(ACTION_POWER, R.string.navring_action_screen_off);
mActions.addAction(ACTION_APP, R.string.select_application);
}
private void setDrawables() {
final ArrayList<TargetDrawable> targets = new ArrayList<TargetDrawable>();
final Context context = getActivity();
if (!mIsLandscape || mIsScreenLarge) {
mStartPosOffset = 1;
mEndPosOffset = 4;
} else {
mStartPosOffset = 3;
mEndPosOffset = 2;
}
// Add Initial Place Holder Targets
for (int i = 0; i < mStartPosOffset; i++) {
targets.add(NavigationRingHelpers.getTargetDrawable(context, null));
}
// Add User Targets
for (int i = 0; i < mTargetActivities.length; i++) {
final TargetDrawable drawable =
NavigationRingHelpers.getTargetDrawable(context, mTargetActivities[i]);
// we also want empty targets to be selectable here
drawable.setEnabled(true);
targets.add(drawable);
}
// Add End Place Holder Targets
for (int i = 0; i < mEndPosOffset; i++) {
targets.add(NavigationRingHelpers.getTargetDrawable(context, null));
}
mGlowPadView.setTargetResources(targets);
NavigationRingHelpers.swapSearchIconIfNeeded(context, mGlowPadView);
}
@Override
public void onResume() {
super.onResume();
// If running on a phone, remove padding around container
if (!mIsScreenLarge) {
mContainer.setPadding(0, 0, 0, 0);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(0, MENU_RESET, 0, R.string.profile_reset_title)
.setIcon(R.drawable.ic_settings_backup)
.setAlphabeticShortcut('r')
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(0, MENU_SAVE, 0, R.string.wifi_save)
.setIcon(R.drawable.ic_menu_save)
.setAlphabeticShortcut('s')
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_RESET:
resetAll();
return true;
case MENU_SAVE:
saveAll();
Toast.makeText(getActivity(), R.string.navring_target_save, Toast.LENGTH_LONG).show();
return true;
default:
return false;
}
}
private void resetAll() {
final AlertDialog d = new AlertDialog.Builder(getActivity())
.setTitle(R.string.lockscreen_target_reset_title)
.setIconAttribute(android.R.attr.alertDialogIcon)
.setMessage(R.string.navring_target_reset_message)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
NavigationRingHelpers.resetActionsToDefaults(getActivity());
updateDrawables();
Toast.makeText(getActivity(),
R.string.navring_target_reset,
Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(R.string.cancel, null)
.create();
d.show();
}
private void saveAll() {
final ContentResolver cr = getActivity().getContentResolver();
for (int i = 0; i < mTargetActivities.length; i++) {
Settings.System.putString(cr,
Settings.System.NAVIGATION_RING_TARGETS[i], mTargetActivities[i]);
}
updateDrawables();
}
@Override
public void shortcutPicked(String uri, String friendlyName, boolean isApplication) {
mTargetActivities[mTargetIndex] = uri;
setDrawables();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mPicker.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
public void updateDrawables() {
mTargetActivities = NavigationRingHelpers.getTargetActions(getActivity());
setDrawables();
}
public void onTargetChange(String uri) {
if (uri.equals(ACTION_APP)) {
final String label = getResources().getString(R.string.lockscreen_target_empty);
final ShortcutIconResource iconResource =
ShortcutIconResource.fromContext(getActivity(), android.R.drawable.ic_delete);
mPicker.pickShortcut(
new String[] { label },
new ShortcutIconResource[] { iconResource },
getId());
} else {
mTargetActivities[mTargetIndex] = uri;
setDrawables();
}
}
@Override
public void onTrigger(View v, final int target) {
mTargetIndex = target - mStartPosOffset;
final DialogInterface.OnClickListener l = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
onTargetChange(mActions.getAction(item));
dialog.dismiss();
}
};
final int selection = mActions.getActionIndex(mTargetActivities[mTargetIndex]);
final AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setTitle(R.string.navring_choose_action_title)
.setSingleChoiceItems(mActions.getEntries(), selection, l)
.create();
dialog.show();
}
@Override
public void onGrabbed(View v, int handle) {
}
@Override
public void onReleased(View v, int handle) {
}
@Override
public void onGrabbedStateChange(View v, int handle) {
}
@Override
public void onFinishFinalAnimation() {
}
}
|