1 package org.connectbot.util; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.content.SharedPreferences; 6 import android.content.res.Configuration; 7 import android.graphics.Canvas; 8 import android.graphics.Paint; 9 import android.os.Bundle; 10 import android.preference.PreferenceManager; 11 import android.view.Menu; 12 import android.view.MenuItem; 13 import android.view.View; 14 import android.view.ViewGroup; 15 import android.view.MenuItem.OnMenuItemClickListener; 16 import android.view.ViewGroup.LayoutParams; 17 import android.widget.AdapterView; 18 import android.widget.BaseAdapter; 19 import android.widget.GridView; 20 import android.widget.LinearLayout; 21 import android.widget.AdapterView.OnItemClickListener; 22 23 import com.googlecode.android_scripting.R; 24 25 import org.connectbot.util.UberColorPickerDialog.OnColorChangedListener; 26 27 /** 28 */ 29 public class ColorsActivity extends Activity implements OnItemClickListener, OnColorChangedListener { 30 31 private SharedPreferences mPreferences; 32 33 private static int sLayoutLanscapeWidth = 400; 34 private static int sLayoutPortraitWidth = 210; 35 36 private GridView mColorGrid; 37 private LinearLayout mLayout; 38 39 private int mFgColor; 40 private int mBgColor; 41 42 private int mCurrentColor = 0; 43 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 mPreferences = PreferenceManager.getDefaultSharedPreferences(this); 49 50 mFgColor = 51 mPreferences.getInt(PreferenceConstants.COLOR_FG, PreferenceConstants.DEFAULT_FG_COLOR); 52 mBgColor = 53 mPreferences.getInt(PreferenceConstants.COLOR_BG, PreferenceConstants.DEFAULT_BG_COLOR); 54 55 setContentView(R.layout.act_colors); 56 57 this.setTitle("Terminal Colors"); 58 59 mLayout = (LinearLayout) findViewById(R.id.color_layout); 60 61 mColorGrid = (GridView) findViewById(R.id.color_grid); 62 mColorGrid.setOnItemClickListener(this); 63 mColorGrid.setSelection(0); 64 65 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 66 mColorGrid.setNumColumns(2); 67 LayoutParams params = mLayout.getLayoutParams(); 68 params.height = params.width; 69 params.width = LayoutParams.WRAP_CONTENT; 70 } 71 mColorGrid.setAdapter(new ColorsAdapter(true)); 72 73 } 74 75 private class ColorsAdapter extends BaseAdapter { 76 private boolean mSquareViews; 77 78 public ColorsAdapter(boolean squareViews) { 79 mSquareViews = squareViews; 80 } 81 82 public View getView(int position, View convertView, ViewGroup parent) { 83 ColorView c; 84 if (convertView == null) { 85 c = new ColorView(ColorsActivity.this, mSquareViews); 86 } else { 87 c = (ColorView) convertView; 88 } 89 if (position == 0) { 90 c.setColor(mFgColor); 91 c.setTitle("Foreground color"); 92 } else { 93 c.setColor(mBgColor); 94 c.setTitle("Background color"); 95 } 96 return c; 97 } 98 99 public int getCount() { 100 return 2; 101 } 102 103 public Object getItem(int position) { 104 return (position == 0) ? mFgColor : mBgColor; 105 } 106 107 public long getItemId(int position) { 108 return position; 109 } 110 } 111 112 private class ColorView extends View { 113 private boolean mSquare; 114 115 private Paint mTextPaint; 116 private Paint mShadowPaint; 117 // Things we paint 118 private int mBackgroundColor; 119 private String mText; 120 121 private int mAscent; 122 private int mWidthCenter; 123 private int mHeightCenter; 124 125 public ColorView(Context context, boolean square) { 126 super(context); 127 128 mSquare = square; 129 130 mTextPaint = new Paint(); 131 mTextPaint.setAntiAlias(true); 132 mTextPaint.setTextSize(16); 133 mTextPaint.setColor(0xFFFFFFFF); 134 mTextPaint.setTextAlign(Paint.Align.CENTER); 135 136 mShadowPaint = new Paint(mTextPaint); 137 mShadowPaint.setStyle(Paint.Style.STROKE); 138 mShadowPaint.setStrokeCap(Paint.Cap.ROUND); 139 mShadowPaint.setStrokeJoin(Paint.Join.ROUND); 140 mShadowPaint.setStrokeWidth(4f); 141 mShadowPaint.setColor(0xFF000000); 142 143 setPadding(20, 20, 20, 20); 144 } 145 146 public void setColor(int color) { 147 mBackgroundColor = color; 148 } 149 150 public void setTitle(String title) { 151 mText = title; 152 } 153 154 @Override 155 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 156 int width = measureWidth(widthMeasureSpec); 157 158 int height; 159 if (mSquare) { 160 height = width; 161 } else { 162 height = measureHeight(heightMeasureSpec); 163 } 164 165 mAscent = (int) mTextPaint.ascent(); 166 mWidthCenter = width / 2; 167 mHeightCenter = height / 2 - mAscent / 2; 168 169 setMeasuredDimension(width, height); 170 } 171 172 private int measureWidth(int measureSpec) { 173 int result = 0; 174 int specMode = MeasureSpec.getMode(measureSpec); 175 int specSize = MeasureSpec.getSize(measureSpec); 176 177 if (specMode == MeasureSpec.EXACTLY) { 178 // We were told how big to be 179 result = specSize; 180 } else { 181 // Measure the text 182 result = (int) mTextPaint.measureText(mText) + getPaddingLeft() + getPaddingRight(); 183 if (specMode == MeasureSpec.AT_MOST) { 184 // Respect AT_MOST value if that was what is called for by 185 // measureSpec 186 result = Math.min(result, specSize); 187 } 188 } 189 190 return result; 191 } 192 193 private int measureHeight(int measureSpec) { 194 int result = 0; 195 int specMode = MeasureSpec.getMode(measureSpec); 196 int specSize = MeasureSpec.getSize(measureSpec); 197 198 mAscent = (int) mTextPaint.ascent(); 199 if (specMode == MeasureSpec.EXACTLY) { 200 // We were told how big to be 201 result = specSize; 202 } else { 203 // Measure the text (beware: ascent is a negative number) 204 result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop() + getPaddingBottom(); 205 if (specMode == MeasureSpec.AT_MOST) { 206 // Respect AT_MOST value if that was what is called for by 207 // measureSpec 208 result = Math.min(result, specSize); 209 } 210 } 211 return result; 212 } 213 214 @Override 215 protected void onDraw(Canvas canvas) { 216 super.onDraw(canvas); 217 canvas.drawColor(mBackgroundColor); 218 canvas.drawText(mText, mWidthCenter, mHeightCenter, mShadowPaint); 219 canvas.drawText(mText, mWidthCenter, mHeightCenter, mTextPaint); 220 } 221 } 222 223 private void editColor(int colorNumber) { 224 mCurrentColor = colorNumber; 225 new UberColorPickerDialog(this, this, (colorNumber == 0) ? mFgColor : mBgColor).show(); 226 } 227 228 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 229 editColor(position); 230 } 231 232 public void onNothingSelected(AdapterView<?> arg0) { 233 } 234 235 public void colorChanged(int value) { 236 SharedPreferences.Editor editor = mPreferences.edit(); 237 if (mCurrentColor == 0) { 238 mFgColor = value; 239 editor.putInt(PreferenceConstants.COLOR_FG, mFgColor); 240 } else { 241 mBgColor = value; 242 editor.putInt(PreferenceConstants.COLOR_BG, mBgColor); 243 } 244 editor.commit(); 245 mColorGrid.invalidateViews(); 246 } 247 248 @Override 249 public void onConfigurationChanged(Configuration newConfig) { 250 super.onConfigurationChanged(newConfig); 251 if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 252 mColorGrid.setNumColumns(2); 253 LayoutParams params = mLayout.getLayoutParams(); 254 params.height = params.width; 255 params.width = sLayoutLanscapeWidth; 256 } else { 257 mColorGrid.setNumColumns(1); 258 LayoutParams params = mLayout.getLayoutParams(); 259 params.height = LayoutParams.WRAP_CONTENT; 260 params.width = sLayoutPortraitWidth; 261 } 262 } 263 264 @Override 265 public boolean onCreateOptionsMenu(Menu menu) { 266 super.onCreateOptionsMenu(menu); 267 MenuItem reset = menu.add("Reset"); 268 reset.setAlphabeticShortcut('r'); 269 reset.setNumericShortcut('1'); 270 reset.setIcon(android.R.drawable.ic_menu_revert); 271 reset.setOnMenuItemClickListener(new OnMenuItemClickListener() { 272 public boolean onMenuItemClick(MenuItem arg0) { 273 mFgColor = PreferenceConstants.DEFAULT_FG_COLOR; 274 mBgColor = PreferenceConstants.DEFAULT_BG_COLOR; 275 SharedPreferences.Editor editor = mPreferences.edit(); 276 editor.putInt(PreferenceConstants.COLOR_FG, mFgColor); 277 editor.putInt(PreferenceConstants.COLOR_BG, mBgColor); 278 editor.commit(); 279 mColorGrid.invalidateViews(); 280 return true; 281 } 282 }); 283 return true; 284 } 285 } 286