• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 com.replica.replicaisland;
18 
19 import android.content.Context;
20 import android.content.DialogInterface;
21 import android.content.SharedPreferences;
22 import android.content.DialogInterface.OnKeyListener;
23 import android.content.res.TypedArray;
24 import android.graphics.drawable.Drawable;
25 import android.os.Bundle;
26 import android.preference.DialogPreference;
27 import android.util.AttributeSet;
28 import android.view.KeyEvent;
29 import android.view.View;
30 import android.widget.TextView;
31 
32 
33 public class KeyboardConfigDialogPreference extends DialogPreference implements OnKeyListener {
34 	private SharedPreferences mSharedPrefs;
35 	private Context mContext;
36 	private String mLeftPrefKey;
37 	private String mRightPrefKey;
38 	private String mJumpPrefKey;
39 	private String mAttackPrefKey;
40 	private String[] mKeyLabels;
41 	private int mListeningId = 0;
42 	private View mLeftBorder;
43 	private View mRightBorder;
44 	private View mJumpBorder;
45 	private View mAttackBorder;
46 	private Drawable mUnselectedBorder;
47 	private Drawable mSelectedBorder;
48 	private int mLeftKeyCode;
49 	private int mRightKeyCode;
50 	private int mJumpKeyCode;
51 	private int mAttackKeyCode;
52 	private TextView mLeftText;
53 	private TextView mRightText;
54 	private TextView mJumpText;
55 	private TextView mAttackText;
56 
57 	private class ConfigClickListener implements View.OnClickListener {
58 		private int mId;
ConfigClickListener(int id)59 		public ConfigClickListener(int id) {
60 			mId = id;
61 		}
62 
onClick(View v)63 		public void onClick(View v) {
64 			selectId(mId);
65 		}
66 
67 	}
68 
KeyboardConfigDialogPreference(Context context, AttributeSet attrs)69 	public KeyboardConfigDialogPreference(Context context, AttributeSet attrs) {
70 		this(context, attrs, android.R.attr.dialogPreferenceStyle);
71 	}
72 
KeyboardConfigDialogPreference(Context context, AttributeSet attrs, int defStyle)73 	public KeyboardConfigDialogPreference(Context context, AttributeSet attrs,
74 			int defStyle) {
75 		super(context, attrs, defStyle);
76 
77 		TypedArray a = context.obtainStyledAttributes(attrs,
78                 R.styleable.KeyConfigPreference, defStyle, 0);
79 		mLeftPrefKey = a.getString(R.styleable.KeyConfigPreference_leftKey);
80 		mRightPrefKey = a.getString(R.styleable.KeyConfigPreference_rightKey);
81 		mJumpPrefKey = a.getString(R.styleable.KeyConfigPreference_jumpKey);
82 		mAttackPrefKey = a.getString(R.styleable.KeyConfigPreference_attackKey);
83 
84         a.recycle();
85 	}
86 
KeyboardConfigDialogPreference(Context context)87 	public KeyboardConfigDialogPreference(Context context) {
88         this(context, null);
89     }
90 
91 	@Override
onBindDialogView(View view)92 	protected void onBindDialogView(View view) {
93 		super.onBindDialogView(view);
94 		if (mSharedPrefs != null) {
95 			mLeftKeyCode = mSharedPrefs.getInt(mLeftPrefKey, KeyEvent.KEYCODE_DPAD_LEFT);
96 			mRightKeyCode = mSharedPrefs.getInt(mRightPrefKey, KeyEvent.KEYCODE_DPAD_RIGHT);
97 			mJumpKeyCode = mSharedPrefs.getInt(mJumpPrefKey, KeyEvent.KEYCODE_SPACE);
98 			mAttackKeyCode = mSharedPrefs.getInt(mAttackPrefKey, KeyEvent.KEYCODE_SHIFT_LEFT);
99 
100 
101 			mLeftText = (TextView)view.findViewById(R.id.key_left);
102 			mLeftText.setText(getKeyLabel(mLeftKeyCode));
103 
104 			mRightText = (TextView)view.findViewById(R.id.key_right);
105 			mRightText.setText(getKeyLabel(mRightKeyCode));
106 
107 			mJumpText = (TextView)view.findViewById(R.id.key_jump);
108 			mJumpText.setText(getKeyLabel(mJumpKeyCode));
109 
110 			mAttackText = (TextView)view.findViewById(R.id.key_attack);
111 			mAttackText.setText(getKeyLabel(mAttackKeyCode));
112 
113 			mLeftBorder = view.findViewById(R.id.left_border);
114 			mRightBorder = view.findViewById(R.id.right_border);
115 			mJumpBorder = view.findViewById(R.id.jump_border);
116 			mAttackBorder = view.findViewById(R.id.attack_border);
117 
118 			mLeftBorder.setOnClickListener(new ConfigClickListener(R.id.key_left));
119 			mRightBorder.setOnClickListener(new ConfigClickListener(R.id.key_right));
120 			mJumpBorder.setOnClickListener(new ConfigClickListener(R.id.key_jump));
121 			mAttackBorder.setOnClickListener(new ConfigClickListener(R.id.key_attack));
122 
123 			mUnselectedBorder = mContext.getResources().getDrawable(R.drawable.key_config_border);
124 			mSelectedBorder = mContext.getResources().getDrawable(R.drawable.key_config_border_active);
125 		}
126 
127 		mListeningId = 0;
128 
129 
130 	}
131 
132 	@Override
showDialog(Bundle state)133 	protected void showDialog(Bundle state) {
134 		super.showDialog(state);
135 		getDialog().setOnKeyListener(this);
136 		getDialog().takeKeyEvents(true);
137 	}
138 
getKeyLabel(int keycode)139 	protected String getKeyLabel(int keycode) {
140 		String result = "Unknown Key";
141 		if (mKeyLabels == null) {
142 			mKeyLabels = mContext.getResources().getStringArray(R.array.keycode_labels);
143 		}
144 
145 		if (keycode > 0 && keycode < mKeyLabels.length) {
146 			result = mKeyLabels[keycode - 1];
147 		}
148 		return result;
149 	}
150 
selectId(int id)151 	public void selectId(int id) {
152 		if (mListeningId != 0) {
153 			// unselect the current box
154 			View border = getConfigViewById(mListeningId);
155 			border.setBackgroundDrawable(mUnselectedBorder);
156 		}
157 
158 		if (id == mListeningId || id == 0) {
159 			mListeningId = 0; // toggle off and end.
160 		} else {
161 			// select the new box
162 			View border = getConfigViewById(id);
163 			border.setBackgroundDrawable(mSelectedBorder);
164 			mListeningId = id;
165 		}
166 	}
167 
getConfigViewById(int id)168 	private View getConfigViewById(int id) {
169 		View config = null;
170 		switch(id) {
171 		case R.id.key_left:
172 			config = mLeftBorder;
173 			break;
174 		case R.id.key_right:
175 			config = mRightBorder;
176 			break;
177 		case R.id.key_jump:
178 			config = mJumpBorder;
179 			break;
180 		case R.id.key_attack:
181 			config = mAttackBorder;
182 			break;
183 		}
184 
185 		return config;
186 	}
187 
188 	@Override
onDialogClosed(boolean positiveResult)189 	protected void onDialogClosed(boolean positiveResult) {
190 		super.onDialogClosed(positiveResult);
191 
192 		if (positiveResult) {
193 			// save changes
194 			SharedPreferences.Editor editor = mSharedPrefs.edit();
195 			editor.putInt(mLeftPrefKey, mLeftKeyCode);
196 			editor.putInt(mRightPrefKey, mRightKeyCode);
197 			editor.putInt(mJumpPrefKey, mJumpKeyCode);
198 			editor.putInt(mAttackPrefKey, mAttackKeyCode);
199 			editor.commit();
200 		}
201 	}
202 
setPrefs(SharedPreferences sharedPreferences)203 	public void setPrefs(SharedPreferences sharedPreferences) {
204 		mSharedPrefs = sharedPreferences;
205 	}
206 
setContext(Context context)207 	public void setContext(Context context) {
208 		mContext = context;
209 	}
210 
onKey(DialogInterface dialog, int keyCode, KeyEvent event)211 	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
212 		boolean eatKey = false;
213 		if (mListeningId != 0) {
214 			eatKey = true;
215 			switch (mListeningId) {
216 			case R.id.key_left:
217 				mLeftText.setText(getKeyLabel(keyCode));
218 				mLeftKeyCode = keyCode;
219 				break;
220 			case R.id.key_right:
221 				mRightText.setText(getKeyLabel(keyCode));
222 				mRightKeyCode = keyCode;
223 				break;
224 			case R.id.key_jump:
225 				mJumpText.setText(getKeyLabel(keyCode));
226 				mJumpKeyCode = keyCode;
227 				break;
228 			case R.id.key_attack:
229 				mAttackText.setText(getKeyLabel(keyCode));
230 				mAttackKeyCode = keyCode;
231 				break;
232 			}
233 
234 			selectId(0);	// deselect the current config box;
235 		}
236 		return eatKey;
237 	}
238 
239 }
240