1 /* 2 * Copyright (C) 2008 The Android Open Source 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 android.text.method; 18 19 import android.app.Dialog; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.text.Editable; 23 import android.text.Selection; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.View.OnClickListener; 27 import android.view.ViewGroup; 28 import android.view.Window; 29 import android.view.WindowManager; 30 import android.widget.AdapterView; 31 import android.widget.AdapterView.OnItemClickListener; 32 import android.widget.BaseAdapter; 33 import android.widget.Button; 34 import android.widget.GridView; 35 36 import com.android.internal.R; 37 38 /** 39 * Dialog for choosing accented characters related to a base character. 40 */ 41 @android.ravenwood.annotation.RavenwoodKeepWholeClass 42 public class CharacterPickerDialog extends Dialog 43 implements OnItemClickListener, OnClickListener { 44 private View mView; 45 private Editable mText; 46 private String mOptions; 47 private boolean mInsert; 48 private LayoutInflater mInflater; 49 private Button mCancelButton; 50 51 /** 52 * Creates a new CharacterPickerDialog that presents the specified 53 * <code>options</code> for insertion or replacement (depending on 54 * the sense of <code>insert</code>) into <code>text</code>. 55 */ CharacterPickerDialog(Context context, View view, Editable text, String options, boolean insert)56 public CharacterPickerDialog(Context context, View view, 57 Editable text, String options, 58 boolean insert) { 59 super(context, com.android.internal.R.style.Theme_Panel); 60 61 mView = view; 62 mText = text; 63 mOptions = options; 64 mInsert = insert; 65 mInflater = LayoutInflater.from(context); 66 } 67 68 @Override onCreate(Bundle savedInstanceState)69 protected void onCreate(Bundle savedInstanceState) { 70 super.onCreate(savedInstanceState); 71 72 WindowManager.LayoutParams params = getWindow().getAttributes(); 73 params.token = mView.getApplicationWindowToken(); 74 params.type = params.TYPE_APPLICATION_ATTACHED_DIALOG; 75 params.flags = params.flags | Window.FEATURE_NO_TITLE; 76 77 setContentView(R.layout.character_picker); 78 79 GridView grid = (GridView) findViewById(R.id.characterPicker); 80 grid.setAdapter(new OptionsAdapter(getContext())); 81 grid.setOnItemClickListener(this); 82 83 mCancelButton = (Button) findViewById(R.id.cancel); 84 mCancelButton.setOnClickListener(this); 85 } 86 87 /** 88 * Handles clicks on the character buttons. 89 */ onItemClick(AdapterView parent, View view, int position, long id)90 public void onItemClick(AdapterView parent, View view, int position, long id) { 91 String result = String.valueOf(mOptions.charAt(position)); 92 replaceCharacterAndClose(result); 93 } 94 replaceCharacterAndClose(CharSequence replace)95 private void replaceCharacterAndClose(CharSequence replace) { 96 int selEnd = Selection.getSelectionEnd(mText); 97 if (mInsert || selEnd == 0) { 98 mText.insert(selEnd, replace); 99 } else { 100 mText.replace(selEnd - 1, selEnd, replace); 101 } 102 103 dismiss(); 104 } 105 106 /** 107 * Handles clicks on the Cancel button. 108 */ onClick(View v)109 public void onClick(View v) { 110 if (v == mCancelButton) { 111 dismiss(); 112 } else if (v instanceof Button) { 113 CharSequence result = ((Button) v).getText(); 114 replaceCharacterAndClose(result); 115 } 116 } 117 118 private class OptionsAdapter extends BaseAdapter { 119 OptionsAdapter(Context context)120 public OptionsAdapter(Context context) { 121 super(); 122 } 123 getView(int position, View convertView, ViewGroup parent)124 public View getView(int position, View convertView, ViewGroup parent) { 125 Button b = (Button) 126 mInflater.inflate(R.layout.character_picker_button, null); 127 b.setText(String.valueOf(mOptions.charAt(position))); 128 b.setOnClickListener(CharacterPickerDialog.this); 129 return b; 130 } 131 getCount()132 public final int getCount() { 133 return mOptions.length(); 134 } 135 getItem(int position)136 public final Object getItem(int position) { 137 return String.valueOf(mOptions.charAt(position)); 138 } 139 getItemId(int position)140 public final long getItemId(int position) { 141 return position; 142 } 143 } 144 } 145