Improve the default activity type preference

This commit is contained in:
Jimmy Shih
2013-11-20 17:49:15 -08:00
parent 0d65e5cafc
commit 41f5206c63
8 changed files with 237 additions and 85 deletions
@@ -0,0 +1,149 @@
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.settings.RecordingSettingsActivity;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.TrackIconUtils;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.os.Parcelable;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
/**
* For entering the default activity type.
*
* @author Jimmy Shih
*/
public class ActivityTypePreference extends DialogPreference {
private RecordingSettingsActivity recordingSettingsActivity;
private AutoCompleteTextView textView;
private Spinner spinner;
public ActivityTypePreference(Context context, AttributeSet attrs) {
super(context, attrs);
setDialogLayoutResource(R.layout.activity_type_preference);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
setDialogIcon(null);
setPersistent(false);
}
@Override
protected View onCreateDialogView() {
View view = super.onCreateDialogView();
textView = (AutoCompleteTextView) view.findViewById(R.id.activity_type_preference_text_view);
spinner = (Spinner) view.findViewById(R.id.activity_type_preference_spinner);
String category = PreferencesUtils.getString(
getContext(), R.string.default_activity_key, PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT);
textView.setText(category);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getContext(), R.array.activity_types, android.R.layout.simple_dropdown_item_1line);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String iconValue = TrackIconUtils.getIconValue(
getContext(), (String) textView.getAdapter().getItem(position));
TrackIconUtils.setIconSpinner(spinner, iconValue);
}
});
textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String iconValue = TrackIconUtils.getIconValue(
getContext(), textView.getText().toString());
TrackIconUtils.setIconSpinner(spinner, iconValue);
}
}
});
String iconValue = TrackIconUtils.getIconValue(getContext(), category);
spinner.setAdapter(TrackIconUtils.getIconSpinnerAdapter(getContext(), iconValue));
spinner.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
recordingSettingsActivity.showChooseActivityTypeDialog();
}
return true;
}
});
spinner.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
recordingSettingsActivity.showChooseActivityTypeDialog();
}
return true;
}
});
return view;
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String value = textView.getText().toString();
if (callChangeListener(value)) {
PreferencesUtils.setString(getContext(), R.string.default_activity_key, value);
}
}
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
super.onRestoreInstanceState(state);
String iconValue = TrackIconUtils.getIconValue(getContext(), textView.getText().toString());
TrackIconUtils.setIconSpinner(spinner, iconValue);
textView.clearFocus();
}
/**
* Sets the recording settings activity.
*
* @param activity the activity
*/
public void setRecordingSettingsActivity(RecordingSettingsActivity activity) {
recordingSettingsActivity = activity;
}
/**
* Updates the value of the dialog.
*
* @param iconValue the icon value
*/
public void updateValue(String iconValue) {
TrackIconUtils.setIconSpinner(spinner, iconValue);
textView.setText(
recordingSettingsActivity.getString(TrackIconUtils.getIconActivityType(iconValue)));
textView.clearFocus();
}
}
@@ -1,65 +0,0 @@
package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
/**
* The {@link AutoCompleteTextPreference} class is a preference that allows for
* string input using auto complete . It is a subclass of
* {@link EditTextPreference} and shows the {@link AutoCompleteTextView} in a
* dialog.
* <p>
* This preference will store a string into the SharedPreferences.
*
* @author Rimas Trumpa (with Matt Levan)
*/
public class AutoCompleteTextPreference extends EditTextPreference {
private AutoCompleteTextView mEditText = null;
public AutoCompleteTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mEditText = new AutoCompleteTextView(context, attrs);
mEditText.setThreshold(0);
// Gets autocomplete values for 'Default Activity' preference
if (PreferencesUtils.getKey(context, R.string.default_activity_key).equals(getKey())) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
context, R.array.activity_types, android.R.layout.simple_dropdown_item_1line);
mEditText.setAdapter(adapter);
}
}
@Override
protected void onBindDialogView(View view) {
AutoCompleteTextView editText = mEditText;
editText.setText(getText());
ViewParent oldParent = editText.getParent();
if (oldParent != view) {
if (oldParent != null) {
((ViewGroup) oldParent).removeView(editText);
}
onAddEditTextToDialogView(view, editText);
}
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String value = mEditText.getText().toString();
if (callChangeListener(value)) {
setText(value);
}
}
}
}
@@ -16,11 +16,15 @@
package com.google.android.apps.mytracks.settings;
import com.google.android.apps.mytracks.ActivityTypePreference;
import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragment;
import com.google.android.apps.mytracks.fragments.ChooseActivityTypeDialogFragment.ChooseActivityTypeCaller;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R;
import android.app.Dialog;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
@@ -30,7 +34,12 @@ import android.preference.Preference;
*
* @author Jimmy Shih
*/
public class RecordingSettingsActivity extends AbstractSettingsActivity {
public class RecordingSettingsActivity extends AbstractSettingsActivity
implements ChooseActivityTypeCaller {
private static final int DIALOG_CHOOSE_ACTIVITY = 0;
private ActivityTypePreference activityTypePreference;
@SuppressWarnings("deprecation")
@Override
@@ -63,6 +72,17 @@ public class RecordingSettingsActivity extends AbstractSettingsActivity {
R.array.auto_resume_track_timeout_values, metricUnits);
}
@Override
protected Dialog onCreateDialog(int id, Bundle bundle) {
if (id != DIALOG_CHOOSE_ACTIVITY) {
return null;
}
String category = PreferencesUtils.getString(
this, R.string.default_activity_key, PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT);
return ChooseActivityTypeDialogFragment.getDialog(this, category, this);
}
@SuppressWarnings("deprecation")
private void configFrequencyPreference(
int key, int defaultValue, int valueArray, boolean metricUnits) {
@@ -85,22 +105,25 @@ public class RecordingSettingsActivity extends AbstractSettingsActivity {
@SuppressWarnings("deprecation")
private void configDefaultActivity() {
Preference preference = findPreference(getString(R.string.default_activity_key));
String value = PreferencesUtils.getString(
activityTypePreference = (ActivityTypePreference) findPreference(
getString(R.string.default_activity_key));
String defaultActivity = PreferencesUtils.getString(
this, R.string.default_activity_key, PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT);
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
String stringValue = (String) newValue;
pref.setSummary(stringValue != null
&& !stringValue.equals(PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT) ? stringValue
: getString(R.string.value_unknown));
return true;
}
});
preference.setSummary(
value != null && !value.equals(PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT) ? value
: getString(R.string.value_unknown));
activityTypePreference.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference pref, Object newValue) {
String stringValue = (String) newValue;
pref.setSummary(stringValue != null
&& !stringValue.equals(PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT) ? stringValue
: getString(R.string.value_unknown));
return true;
}
});
activityTypePreference.setSummary(defaultActivity != null
&& !defaultActivity.equals(PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT) ? defaultActivity
: getString(R.string.value_unknown));
activityTypePreference.setRecordingSettingsActivity(this);
}
@SuppressWarnings("deprecation")
@@ -335,4 +358,13 @@ public class RecordingSettingsActivity extends AbstractSettingsActivity {
}
}
}
public void showChooseActivityTypeDialog() {
showDialog(DIALOG_CHOOSE_ACTIVITY);
}
@Override
public void onChooseActivityTypeDone(String iconValue, boolean newWeight) {
activityTypePreference.updateValue(iconValue);
}
}