1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.tuner; 16 17 import android.app.AlertDialog; 18 import android.content.Context; 19 import android.content.DialogInterface; 20 import android.content.Intent; 21 import android.view.KeyEvent; 22 23 import com.android.systemui.R; 24 25 import java.lang.reflect.Field; 26 import java.lang.reflect.Modifier; 27 import java.util.ArrayList; 28 29 public class KeycodeSelectionHelper { 30 31 private static final ArrayList<String> mKeycodeStrings = new ArrayList<>(); 32 private static final ArrayList<Integer> mKeycodes = new ArrayList<>(); 33 34 private static final String KEYCODE_STRING = "KEYCODE_"; 35 36 static { 37 Class<KeyEvent> cls = KeyEvent.class; 38 for (Field field : cls.getDeclaredFields()) { 39 if (Modifier.isStatic(field.getModifiers()) 40 && field.getName().startsWith(KEYCODE_STRING) 41 && field.getType().equals(int.class)) { 42 try { 43 mKeycodeStrings.add(formatString(field.getName())); 44 mKeycodes.add((Integer) field.get(null)); 45 } catch (IllegalAccessException e) { 46 } 47 } 48 } 49 } 50 51 // Force the string into something somewhat readable. 52 private static String formatString(String name) { 53 StringBuilder str = new StringBuilder(name.replace(KEYCODE_STRING, "").replace("_", " ") 54 .toLowerCase()); 55 for (int i = 0; i < str.length(); i++) { 56 if (i == 0 || str.charAt(i - 1) == ' ') { 57 str.setCharAt(i, Character.toUpperCase(str.charAt(i))); 58 } 59 } 60 return str.toString(); 61 } 62 63 public static void showKeycodeSelect(Context context, final OnSelectionComplete listener) { 64 new AlertDialog.Builder(context) 65 .setTitle(R.string.select_keycode) 66 .setItems(mKeycodeStrings.toArray(new String[0]), 67 new DialogInterface.OnClickListener() { 68 @Override 69 public void onClick(DialogInterface dialog, int which) { 70 listener.onSelectionComplete(mKeycodes.get(which)); 71 } 72 }).show(); 73 } 74 75 public static Intent getSelectImageIntent() { 76 return new Intent(Intent.ACTION_OPEN_DOCUMENT).addCategory(Intent.CATEGORY_OPENABLE) 77 .setType("image/*"); 78 } 79 80 public interface OnSelectionComplete { 81 void onSelectionComplete(int code); 82 } 83 } 84