comparison app/src/main/java/com/five_ten_sg/connectbot/ColorsActivity.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/com/five_ten_sg/connectbot/ColorsActivity.java@0ce5cc452d02
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /*
2 * ConnectBot: simple, powerful, open-source SSH client for Android
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package com.five_ten_sg.connectbot;
19
20 import java.util.Arrays;
21 import java.util.List;
22
23 import com.five_ten_sg.connectbot.util.Colors;
24 import com.five_ten_sg.connectbot.util.HostDatabase;
25 import com.five_ten_sg.connectbot.util.UberColorPickerDialog;
26 import com.five_ten_sg.connectbot.util.UberColorPickerDialog.OnColorChangedListener;
27 import android.app.Activity;
28 import android.content.Context;
29 import android.graphics.Canvas;
30 import android.graphics.Paint;
31 import android.os.Bundle;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.MenuItem.OnMenuItemClickListener;
35 import android.view.View;
36 import android.view.ViewGroup;
37 import android.widget.AdapterView;
38 import android.widget.AdapterView.OnItemClickListener;
39 import android.widget.AdapterView.OnItemSelectedListener;
40 import android.widget.BaseAdapter;
41 import android.widget.GridView;
42 import android.widget.Spinner;
43
44 /**
45 * @author Kenny Root
46 *
47 */
48 public class ColorsActivity extends Activity implements OnItemClickListener, OnColorChangedListener, OnItemSelectedListener {
49 private GridView mColorGrid;
50 private Spinner mFgSpinner;
51 private Spinner mBgSpinner;
52
53 private int mColorScheme;
54
55 private List<Integer> mColorList;
56 private HostDatabase hostdb;
57
58 private int mCurrentColor = 0;
59
60 private int[] mDefaultColors;
61 @Override
62 protected void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState);
64 setContentView(R.layout.act_colors);
65 this.setTitle(String.format("%s: %s",
66 getResources().getText(R.string.app_name),
67 getResources().getText(R.string.title_colors)));
68 mColorScheme = HostDatabase.DEFAULT_COLOR_SCHEME;
69 hostdb = new HostDatabase(this);
70 mColorList = Arrays.asList(hostdb.getColorsForScheme(mColorScheme));
71 mDefaultColors = hostdb.getDefaultColorsForScheme(mColorScheme);
72 mColorGrid = (GridView) findViewById(R.id.color_grid);
73 mColorGrid.setAdapter(new ColorsAdapter(true));
74 mColorGrid.setOnItemClickListener(this);
75 mColorGrid.setSelection(0);
76 mFgSpinner = (Spinner) findViewById(R.id.fg);
77 mFgSpinner.setAdapter(new ColorsAdapter(false));
78 mFgSpinner.setSelection(mDefaultColors[0]);
79 mFgSpinner.setOnItemSelectedListener(this);
80 mBgSpinner = (Spinner) findViewById(R.id.bg);
81 mBgSpinner.setAdapter(new ColorsAdapter(false));
82 mBgSpinner.setSelection(mDefaultColors[1]);
83 mBgSpinner.setOnItemSelectedListener(this);
84 }
85
86 @Override
87 protected void onDestroy() {
88 super.onDestroy();
89
90 if (hostdb != null) {
91 hostdb.close();
92 hostdb = null;
93 }
94 }
95
96 @Override
97 protected void onResume() {
98 super.onResume();
99
100 if (hostdb == null)
101 hostdb = new HostDatabase(this);
102 }
103
104 private class ColorsAdapter extends BaseAdapter {
105 private boolean mSquareViews;
106
107 public ColorsAdapter(boolean squareViews) {
108 mSquareViews = squareViews;
109 }
110
111 public View getView(int position, View convertView, ViewGroup parent) {
112 ColorView c;
113
114 if (convertView == null) {
115 c = new ColorView(ColorsActivity.this, mSquareViews);
116 }
117 else {
118 c = (ColorView) convertView;
119 }
120
121 c.setColor(mColorList.get(position));
122 c.setNumber(position + 1);
123 return c;
124 }
125
126 public int getCount() {
127 return mColorList.size();
128 }
129
130 public Object getItem(int position) {
131 return mColorList.get(position);
132 }
133
134 public long getItemId(int position) {
135 return position;
136 }
137 }
138
139 private class ColorView extends View {
140 private boolean mSquare;
141
142 private Paint mTextPaint;
143 private Paint mShadowPaint;
144
145 // Things we paint
146 private int mBackgroundColor;
147 private String mText;
148
149 private int mAscent;
150 private int mWidthCenter;
151 private int mHeightCenter;
152
153 public ColorView(Context context, boolean square) {
154 super(context);
155 mSquare = square;
156 mTextPaint = new Paint();
157 mTextPaint.setAntiAlias(true);
158 mTextPaint.setTextSize(16);
159 mTextPaint.setColor(0xFFFFFFFF);
160 mTextPaint.setTextAlign(Paint.Align.CENTER);
161 mShadowPaint = new Paint(mTextPaint);
162 mShadowPaint.setStyle(Paint.Style.STROKE);
163 mShadowPaint.setStrokeCap(Paint.Cap.ROUND);
164 mShadowPaint.setStrokeJoin(Paint.Join.ROUND);
165 mShadowPaint.setStrokeWidth(4f);
166 mShadowPaint.setColor(0xFF000000);
167 setPadding(10, 10, 10, 10);
168 }
169
170 public void setColor(int color) {
171 mBackgroundColor = color;
172 }
173
174 public void setNumber(int number) {
175 mText = Integer.toString(number);
176 }
177
178 @Override
179 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
180 int width = measureWidth(widthMeasureSpec);
181 int height;
182
183 if (mSquare)
184 height = width;
185 else
186 height = measureHeight(heightMeasureSpec);
187
188 mAscent = (int) mTextPaint.ascent();
189 mWidthCenter = width / 2;
190 mHeightCenter = height / 2 - mAscent / 2;
191 setMeasuredDimension(width, height);
192 }
193
194 private int measureWidth(int measureSpec) {
195 int result = 0;
196 int specMode = MeasureSpec.getMode(measureSpec);
197 int specSize = MeasureSpec.getSize(measureSpec);
198
199 if (specMode == MeasureSpec.EXACTLY) {
200 // We were told how big to be
201 result = specSize;
202 }
203 else {
204 // Measure the text
205 result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
206 + getPaddingRight();
207
208 if (specMode == MeasureSpec.AT_MOST) {
209 // Respect AT_MOST value if that was what is called for by
210 // measureSpec
211 result = Math.min(result, specSize);
212 }
213 }
214
215 return result;
216 }
217
218 private int measureHeight(int measureSpec) {
219 int result = 0;
220 int specMode = MeasureSpec.getMode(measureSpec);
221 int specSize = MeasureSpec.getSize(measureSpec);
222 mAscent = (int) mTextPaint.ascent();
223
224 if (specMode == MeasureSpec.EXACTLY) {
225 // We were told how big to be
226 result = specSize;
227 }
228 else {
229 // Measure the text (beware: ascent is a negative number)
230 result = (int)(-mAscent + mTextPaint.descent())
231 + getPaddingTop() + getPaddingBottom();
232
233 if (specMode == MeasureSpec.AT_MOST) {
234 // Respect AT_MOST value if that was what is called for by
235 // measureSpec
236 result = Math.min(result, specSize);
237 }
238 }
239
240 return result;
241 }
242
243
244 @Override
245 protected void onDraw(Canvas canvas) {
246 super.onDraw(canvas);
247 canvas.drawColor(mBackgroundColor);
248 canvas.drawText(mText, mWidthCenter, mHeightCenter, mShadowPaint);
249 canvas.drawText(mText, mWidthCenter, mHeightCenter, mTextPaint);
250 }
251 }
252
253 private void editColor(int colorNumber) {
254 mCurrentColor = colorNumber;
255 new UberColorPickerDialog(this, this, mColorList.get(colorNumber)).show();
256 }
257
258 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
259 editColor(position);
260 }
261
262 public void onNothingSelected(AdapterView<?> arg0) { }
263
264 public void colorChanged(int value) {
265 hostdb.setGlobalColor(mCurrentColor, value);
266 mColorList.set(mCurrentColor, value);
267 mColorGrid.invalidateViews();
268 }
269
270 public void onItemSelected(AdapterView<?> parent, View view, int position,
271 long id) {
272 boolean needUpdate = false;
273
274 if (parent == mFgSpinner) {
275 if (position != mDefaultColors[0]) {
276 mDefaultColors[0] = position;
277 needUpdate = true;
278 }
279 }
280 else if (parent == mBgSpinner) {
281 if (position != mDefaultColors[1]) {
282 mDefaultColors[1] = position;
283 needUpdate = true;
284 }
285 }
286
287 if (needUpdate)
288 hostdb.setDefaultColorsForScheme(mColorScheme, mDefaultColors[0], mDefaultColors[1]);
289 }
290
291 @Override
292 public boolean onCreateOptionsMenu(Menu menu) {
293 super.onCreateOptionsMenu(menu);
294 MenuItem reset = menu.add(R.string.menu_colors_reset);
295 reset.setAlphabeticShortcut('r');
296 reset.setNumericShortcut('1');
297 reset.setIcon(android.R.drawable.ic_menu_revert);
298 reset.setOnMenuItemClickListener(new OnMenuItemClickListener() {
299 public boolean onMenuItemClick(MenuItem arg0) {
300 // Reset each individual color to defaults.
301 for (int i = 0; i < Colors.defaults.length; i++) {
302 if (mColorList.get(i) != Colors.defaults[i]) {
303 hostdb.setGlobalColor(i, Colors.defaults[i]);
304 mColorList.set(i, Colors.defaults[i]);
305 }
306 }
307
308 mColorGrid.invalidateViews();
309 // Reset the default FG/BG colors as well.
310 mFgSpinner.setSelection(HostDatabase.DEFAULT_FG_COLOR);
311 mBgSpinner.setSelection(HostDatabase.DEFAULT_BG_COLOR);
312 hostdb.setDefaultColorsForScheme(HostDatabase.DEFAULT_COLOR_SCHEME,
313 HostDatabase.DEFAULT_FG_COLOR, HostDatabase.DEFAULT_BG_COLOR);
314 return true;
315 }
316 });
317 return true;
318 }
319 }