annotate src/com/five_ten_sg/connectbot/util/StringPickerDialog.java @ 252:932e34a11e9e

add range checking on get/set fields
author Carl Byington <carl@five-ten-sg.com>
date Mon, 14 Jul 2014 12:24:29 -0700
parents 61ed3984fc1d
children 071eccdff8ea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
223
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /*
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * Copyright (C) 2008 The Android Open Source Project
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 *
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 * Licensed under the Apache License, Version 2.0 (the "License");
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 * you may not use this file except in compliance with the License.
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 * You may obtain a copy of the License at
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 *
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 * http://www.apache.org/licenses/LICENSE-2.0
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 *
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 * Unless required by applicable law or agreed to in writing, software
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 * distributed under the License is distributed on an "AS IS" BASIS,
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 * See the License for the specific language governing permissions and
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 * limitations under the License.
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 */
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 package com.five_ten_sg.connectbot.util;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 import com.five_ten_sg.connectbot.R;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 import android.app.Dialog;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 import android.content.Context;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 import android.os.Bundle;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 import android.text.*;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 import android.view.LayoutInflater;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 import android.view.View.OnClickListener;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 import android.view.View;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28 import android.view.ViewGroup;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 import android.view.Window;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 import android.view.WindowManager;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 import android.widget.AdapterView.OnItemClickListener;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 import android.widget.AdapterView;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33 import android.widget.BaseAdapter;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 import android.widget.Button;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 import android.widget.GridView;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 /**
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 * Dialog for choosing accented characters related to a base character.
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 */
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 public class StringPickerDialog extends Dialog
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 implements OnItemClickListener, OnClickListener {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 private View mView;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 private Editable mText;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 private String []mOptions;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 private boolean mInsert;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 private LayoutInflater mInflater;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 private Button mCancelButton;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 /**
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 * Creates a new StringPickerDialog that presents the specified
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 * <code>options</code> for insertion or replacement (depending on
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 * the sense of <code>insert</code>) into <code>text</code>.
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 */
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 public StringPickerDialog(Context context, View view,
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 Editable text, String []options,
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 boolean insert) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57 //super(context, com.android.internal.R.style.Theme_Panel);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 //Resources res = Resources.getSystem();
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 //int id = res.getIdentifier("Theme_Panel", "style", "android");
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 //int id = android.R.style.Theme_Panel;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62 super(context, android.R.style.Theme_Panel);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 mView = view;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64 mText = text;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 mOptions = options;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 mInsert = insert;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67 mInflater = LayoutInflater.from(context);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70 @Override
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71 protected void onCreate(Bundle savedInstanceState) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 super.onCreate(savedInstanceState);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 WindowManager.LayoutParams params = getWindow().getAttributes();
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 params.token = mView.getApplicationWindowToken();
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 params.type = params.TYPE_APPLICATION_ATTACHED_DIALOG;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 params.flags = params.flags | Window.FEATURE_NO_TITLE;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 setContentView(R.layout.string_picker);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 GridView grid = (GridView) findViewById(R.id.stringPicker);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 grid.setAdapter(new OptionsAdapter(getContext()));
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 grid.setOnItemClickListener(this);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 mCancelButton = (Button) findViewById(R.id.cancel);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 mCancelButton.setOnClickListener(this);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 /**
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 * Handles clicks on the character buttons.
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 */
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 public void onItemClick(AdapterView parent, View view, int position, long id) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 String result = mOptions[position];
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94 replaceCharacterAndClose(result);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 private void replaceCharacterAndClose(CharSequence replace) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 int selEnd = Selection.getSelectionEnd(mText);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99 if (mInsert || selEnd == 0) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100 mText.insert(selEnd, replace);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101 } else {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 mText.replace(selEnd - 1, selEnd, replace);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
105 dismiss();
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108 /**
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 * Handles clicks on the Cancel button.
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 */
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 public void onClick(View v) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112 if (v == mCancelButton) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113 dismiss();
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114 } else if (v instanceof Button) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115 CharSequence result = ((Button) v).getText();
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116 replaceCharacterAndClose(result);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120 private class OptionsAdapter extends BaseAdapter {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
121
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
122 public OptionsAdapter(Context context) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123 super();
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
124 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
125
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
126 public View getView(int position, View convertView, ViewGroup parent) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
127 Button b = (Button)
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 mInflater.inflate(R.layout.string_picker_button, null);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
129 b.setText(mOptions[position]);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130 b.setOnClickListener(StringPickerDialog.this);
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131 return b;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
132 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
133
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134 public final int getCount() {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
135 return mOptions.length;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
136 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
138 public final Object getItem(int position) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
139 return mOptions[position];
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
140 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142 public final long getItemId(int position) {
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143 return position;
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
144 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
145 }
61ed3984fc1d proper labels on the soft 24 function keypad
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
146 }