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,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="16dp" >
<AutoCompleteTextView
android:hint="@string/track_edit_activity_type_hint"
android:id="@+id/activity_type_preference_text_view"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
style="@style/EditCategory" />
<Spinner
android:id="@+id/activity_type_preference_spinner"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
+1
View File
@@ -57,6 +57,7 @@ limitations under the License.
android:hint="@string/marker_edit_marker_type_hint"
android:id="@+id/marker_edit_waypoint_marker_type"
android:imeOptions="actionNext"
android:layout_width="match_parent"
style="@style/EditCategory" />
<TextView
android:text="@string/generic_description"
-2
View File
@@ -40,11 +40,9 @@ limitations under the License.
android:layout_height="wrap_content"
android:layout_width="match_parent" >
<AutoCompleteTextView
android:gravity="bottom"
android:hint="@string/track_edit_activity_type_hint"
android:id="@+id/track_edit_activity_type"
android:imeOptions="actionNext"
android:layout_gravity="bottom"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
+3
View File
@@ -39,6 +39,9 @@ limitations under the License.
</style>
<!-- edit category -->
<style name="EditCategory" parent="@style/TextMedium">
<item name="android:completionThreshold">1</item>
<item name="android:gravity">bottom</item>
<item name="android:layout_gravity">bottom</item>
<item name="android:layout_width">match_parent</item>
<item name="android:selectAllOnFocus">true</item>
<item name="android:singleLine">true</item>
+1 -2
View File
@@ -28,9 +28,8 @@ limitations under the License.
android:defaultValue="@string/settings_recording_track_name_location_value"
android:key="@string/track_name_key"
android:title="@string/settings_recording_track_name_title" />
<com.google.android.apps.mytracks.AutoCompleteTextPreference
<com.google.android.apps.mytracks.ActivityTypePreference
android:defaultValue=""
android:hint="@string/track_edit_activity_type_hint"
android:key="@string/default_activity_key"
android:title="@string/settings_recording_default_activity_title" />
<com.google.android.apps.mytracks.IntegerListPreference
@@ -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);
}
}