diff options
author | Nebojsa Cvetkovic <nebkat@gmail.com> | 2013-11-20 21:26:10 +0000 |
---|---|---|
committer | Danesh M <daneshm90@gmail.com> | 2014-01-24 16:24:21 -0800 |
commit | 088187ee79416506852262535fca8963611fafbc (patch) | |
tree | 147e9bb6a183252cb49a765bb6df6c792fffe7f0 | |
parent | b5a2742f4196a2aa20217e64fd5bcccd19ad2a59 (diff) | |
download | android_packages_apps_Trebuchet-088187ee79416506852262535fca8963611fafbc.tar.gz android_packages_apps_Trebuchet-088187ee79416506852262535fca8963611fafbc.tar.bz2 android_packages_apps_Trebuchet-088187ee79416506852262535fca8963611fafbc.zip |
Settings: Initial
Change-Id: If8199313561dfba03a1aad0b49bb4036dcf586c9
-rw-r--r-- | AndroidManifest.xml | 13 | ||||
-rw-r--r-- | res/layout/preference_header_item.xml | 66 | ||||
-rw-r--r-- | res/values/cm_strings.xml | 31 | ||||
-rw-r--r-- | res/xml/preferences_headers.xml | 38 | ||||
-rw-r--r-- | src/com/android/launcher3/Launcher.java | 32 | ||||
-rw-r--r-- | src/com/android/launcher3/settings/SettingsActivity.java | 225 | ||||
-rw-r--r-- | src/com/android/launcher3/settings/SettingsProvider.java | 50 |
7 files changed, 454 insertions, 1 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 901b63838..6d28b66d1 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -19,7 +19,10 @@ --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" - package="com.android.launcher3"> + package="com.android.launcher3" + android:versionName="@string/application_version"> + + <uses-sdk android:minSdkVersion="18" android:targetSdkVersion="19" /> <permission android:name="com.android.launcher3.permission.PRELOAD_WORKSPACE" @@ -91,6 +94,14 @@ </intent-filter> </activity> + <activity android:name="com.android.launcher3.settings.SettingsActivity" + android:label="@string/preferences_title"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="com.cyanogenmod.category.LAUNCHER_PREFERENCES" /> + </intent-filter> + </activity> + <activity android:name="com.android.launcher3.ToggleWeightWatcher" android:label="@string/toggle_weight_watcher" diff --git a/res/layout/preference_header_item.xml b/res/layout/preference_header_item.xml new file mode 100644 index 000000000..24a8ad71c --- /dev/null +++ b/res/layout/preference_header_item.xml @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2012 The Android Open Source Project + + 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. +--> + +<!-- Layout of a header item in PreferenceActivity. --> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:minHeight="48dp" + android:background="?android:attr/activatedBackgroundIndicator" + android:gravity="center_vertical" + android:paddingEnd="?android:attr/scrollbarSize"> + + <LinearLayout + android:layout_width="wrap_content" + android:layout_marginStart="6dip" + android:layout_marginEnd="6dip" + android:layout_height="wrap_content"> + <ImageView + android:id="@android:id/icon" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center" /> + </LinearLayout> + + <RelativeLayout + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginStart="2dip" + android:layout_marginEnd="6dip" + android:layout_marginTop="6dip" + android:layout_marginBottom="6dip" + android:layout_weight="1"> + + <TextView android:id="@android:id/title" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:singleLine="true" + android:textAppearance="?android:attr/textAppearanceMedium" + android:ellipsize="marquee" + android:fadingEdge="horizontal" /> + + <TextView android:id="@android:id/summary" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_below="@android:id/title" + android:layout_alignStart="@android:id/title" + android:textAppearance="?android:attr/textAppearanceSmall" + android:ellipsize="end" + android:maxLines="2" /> + + </RelativeLayout> + +</LinearLayout> diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml new file mode 100644 index 000000000..4da459fd2 --- /dev/null +++ b/res/values/cm_strings.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources> + <!-- App copyright --> + <string name="application_copyright">Copyright \u00A9 2013 The CyanogenMod Project</string> + <!-- App version --> + <string name="application_version" translatable="false">0.1</string> + + <!-- Settings --> + <string name="preferences_title">Settings</string> + <!-- UI --> + <string name="preferences_interface_title">Interface</string> + <!-- Application --> + <string name="preferences_application_title">App</string> + + <!-- UI --> + <!-- Homescreen --> + <string name="preferences_interface_homescreen_title">Homescreen</string> + <string name="preferences_interface_homescreen_summary">Screens and wallpaper</string> + + <!-- Drawer --> + <string name="preferences_interface_drawer_title">Drawer</string> + + <!-- Dock --> + <string name="preferences_interface_dock_title">Dock</string> + + <!-- Icons --> + <string name="preferences_interface_icons_title">Icons</string> + + <!-- General --> + <string name="preferences_interface_general_title">General</string> +</resources>
\ No newline at end of file diff --git a/res/xml/preferences_headers.xml b/res/xml/preferences_headers.xml new file mode 100644 index 000000000..c1975cabc --- /dev/null +++ b/res/xml/preferences_headers.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2009 The Android Open Source Project + + 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. +--> + +<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> + + <header android:id="@+id/preferences_homescreen_section" + android:title="@string/preferences_interface_homescreen_title" + android:summary="@string/preferences_interface_homescreen_summary" /> + + <header android:id="@+id/preferences_drawer_section" + android:title="@string/preferences_interface_drawer_title" /> + + <header android:id="@+id/preferences_dock_section" + android:title="@string/preferences_interface_dock_title" /> + + <header android:id="@+id/preferences_general_section" + android:title="@string/preferences_interface_general_title" /> + + <header android:id="@+id/preferences_application_section" + android:title="@string/preferences_application_title" /> + + <header android:id="@+id/preferences_application_version" + android:summary="@string/application_copyright" /> + +</preference-headers>
\ No newline at end of file diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index b0e49685d..fb9e1da80 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -98,6 +98,8 @@ import android.widget.TextView; import android.widget.Toast; import com.android.launcher3.DropTarget.DragObject; +import com.android.launcher3.settings.SettingsActivity; +import com.android.launcher3.settings.SettingsProvider; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -406,6 +408,9 @@ public class Launcher extends Activity mDragController = new DragController(this); mInflater = getLayoutInflater(); + // Load all settings + SettingsProvider.load(this); + mStats = new Stats(this); mAppWidgetManager = AppWidgetManager.getInstance(this); @@ -833,6 +838,10 @@ public class Launcher extends Activity } super.onResume(); + if (settingsChanged()) { + finish(); + } + // Restore the previous launcher state if (mOnResumeState == State.WORKSPACE) { showWorkspace(false); @@ -978,6 +987,13 @@ public class Launcher extends Activity } protected void startSettings() { + Intent settings = new Intent().setClass(this, SettingsActivity.class); + settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); + startActivity(settings); + if (mWorkspace.isInOverviewMode()) { + mWorkspace.exitOverviewMode(false); + } } public interface QSBScroller { @@ -4381,6 +4397,22 @@ public class Launcher extends Activity } /** + * To avoid managing preference change listeners for various parts of the + * launcher we simply kill the process and let it reload from scratch. + */ + public boolean settingsChanged() { + SharedPreferences prefs = + getSharedPreferences(SettingsProvider.SETTINGS_KEY, Context.MODE_PRIVATE); + boolean settingsChanged = prefs.getBoolean(SettingsProvider.SETTINGS_CHANGED, false); + if (settingsChanged) { + SharedPreferences.Editor editor = prefs.edit(); + editor.putBoolean(SettingsProvider.SETTINGS_CHANGED, false); + editor.commit(); + } + return settingsChanged; + } + + /** * Prints out out state for debugging. */ public void dumpState() { diff --git a/src/com/android/launcher3/settings/SettingsActivity.java b/src/com/android/launcher3/settings/SettingsActivity.java new file mode 100644 index 000000000..e9cf6fe1a --- /dev/null +++ b/src/com/android/launcher3/settings/SettingsActivity.java @@ -0,0 +1,225 @@ +/* + * Copyright (C) 2013 The CyanogenMod Project + * + * 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.android.launcher3.settings; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.*; +import android.text.TextUtils; +import android.view.MenuItem; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.ListAdapter; +import android.widget.TextView; +import com.android.launcher3.R; + +import java.util.List; + +public class SettingsActivity extends PreferenceActivity + implements SharedPreferences.OnSharedPreferenceChangeListener { + private static final String TAG = "Launcher3.SettingsActivity"; + + private SharedPreferences mSettings; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mSettings = getSharedPreferences(SettingsProvider.SETTINGS_KEY, + Context.MODE_PRIVATE); + + getActionBar().setDisplayHomeAsUpEnabled(true); + } + + @Override + protected void onResume() { + super.onResume(); + mSettings.registerOnSharedPreferenceChangeListener(this); + } + + @Override + protected void onPause() { + super.onPause(); + mSettings.unregisterOnSharedPreferenceChangeListener(this); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + onBackPressed(); + finish(); + return true; + default: + break; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onBuildHeaders(List<Header> target) { + loadHeadersFromResource(R.xml.preferences_headers, target); + updateHeaders(target); + } + + private void updateHeaders(List<Header> headers) { + int i = 0; + while (i < headers.size()) { + Header header = headers.get(i); + + // Version preference + if (header.id == R.id.preferences_application_version) { + header.title = getString(R.string.application_name) + " " + getString(R.string.application_version); + } + + // Increment if not removed + if (headers.get(i) == header) { + i++; + } + } + } + + @Override + public void setListAdapter(ListAdapter adapter) { + if (adapter == null) { + super.setListAdapter(null); + } else { + super.setListAdapter(new HeaderAdapter(this, getHeaders())); + } + } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + SharedPreferences.Editor editor = mSettings.edit(); + editor.putBoolean(SettingsProvider.SETTINGS_CHANGED, true); + editor.commit(); + } + + private static class HeaderAdapter extends ArrayAdapter<Header> { + private static final int HEADER_TYPE_NORMAL = 0; + private static final int HEADER_TYPE_CATEGORY = 1; + + private static final int HEADER_TYPE_COUNT = HEADER_TYPE_CATEGORY + 1; + + private static class HeaderViewHolder { + ImageView icon; + TextView title; + TextView summary; + } + + private LayoutInflater mInflater; + + static int getHeaderType(Header header) { + if (header.id == R.id.preferences_application_section) { + return HEADER_TYPE_CATEGORY; + } else { + return HEADER_TYPE_NORMAL; + } + } + + @Override + public int getItemViewType(int position) { + Header header = getItem(position); + return getHeaderType(header); + } + + @Override + public boolean areAllItemsEnabled() { + return false; // because of categories + } + + @Override + public boolean isEnabled(int position) { + return getItemViewType(position) != HEADER_TYPE_CATEGORY; + } + + @Override + public int getViewTypeCount() { + return HEADER_TYPE_COUNT; + } + + @Override + public boolean hasStableIds() { + return true; + } + + public HeaderAdapter(Context context, List<Header> objects) { + super(context, 0, objects); + + mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + HeaderViewHolder holder; + Header header = getItem(position); + int headerType = getHeaderType(header); + View view = null; + + if (convertView == null) { + holder = new HeaderViewHolder(); + switch (headerType) { + case HEADER_TYPE_CATEGORY: + view = new TextView(getContext(), null, + android.R.attr.listSeparatorTextViewStyle); + holder.title = (TextView) view; + break; + + case HEADER_TYPE_NORMAL: + view = mInflater.inflate( + R.layout.preference_header_item, parent, + false); + holder.icon = (ImageView) view.findViewById(android.R.id.icon); + holder.title = (TextView) + view.findViewById(android.R.id.title); + holder.summary = (TextView) + view.findViewById(android.R.id.summary); + break; + } + view.setTag(holder); + } else { + view = convertView; + holder = (HeaderViewHolder) view.getTag(); + } + + // All view fields must be updated every time, because the view may be recycled + switch (headerType) { + case HEADER_TYPE_CATEGORY: + holder.title.setText(header.getTitle(getContext().getResources())); + break; + + case HEADER_TYPE_NORMAL: + holder.icon.setImageResource(header.iconRes); + holder.title.setText(header.getTitle(getContext().getResources())); + CharSequence summary = header.getSummary(getContext().getResources()); + if (!TextUtils.isEmpty(summary)) { + holder.summary.setVisibility(View.VISIBLE); + holder.summary.setText(summary); + } else { + holder.summary.setVisibility(View.GONE); + } + break; + } + + return view; + } + } +} diff --git a/src/com/android/launcher3/settings/SettingsProvider.java b/src/com/android/launcher3/settings/SettingsProvider.java new file mode 100644 index 000000000..30a7732b9 --- /dev/null +++ b/src/com/android/launcher3/settings/SettingsProvider.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2013 The CyanogenMod Project + * + * 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.android.launcher3.settings; + +import android.content.Context; +import android.content.SharedPreferences; + +import java.util.Map; + +public final class SettingsProvider { + public static final String SETTINGS_KEY = "com.android.launcher3_preferences"; + + public static final String SETTINGS_CHANGED = "settings_changed"; + + private static Map<String, ?> sKeyValues; + + public static void load(Context context) { + SharedPreferences preferences = context.getSharedPreferences(SETTINGS_KEY, 0); + sKeyValues = preferences.getAll(); + } + + private static int getInt(String key, int def) { + return sKeyValues.containsKey(key) && sKeyValues.get(key) instanceof Integer ? + (Integer) sKeyValues.get(key) : def; + } + + private static boolean getBoolean(String key, boolean def) { + return sKeyValues.containsKey(key) && sKeyValues.get(key) instanceof Boolean ? + (Boolean) sKeyValues.get(key) : def; + } + + private static String getString(String key, String def) { + return sKeyValues.containsKey(key) && sKeyValues.get(key) instanceof String ? + (String) sKeyValues.get(key) : def; + } +} |