summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/settings
diff options
context:
space:
mode:
authorNebojsa Cvetkovic <nebkat@gmail.com>2013-11-20 21:26:10 +0000
committerDanesh M <daneshm90@gmail.com>2014-01-24 16:24:21 -0800
commit088187ee79416506852262535fca8963611fafbc (patch)
tree147e9bb6a183252cb49a765bb6df6c792fffe7f0 /src/com/android/launcher3/settings
parentb5a2742f4196a2aa20217e64fd5bcccd19ad2a59 (diff)
downloadandroid_packages_apps_Trebuchet-088187ee79416506852262535fca8963611fafbc.tar.gz
android_packages_apps_Trebuchet-088187ee79416506852262535fca8963611fafbc.tar.bz2
android_packages_apps_Trebuchet-088187ee79416506852262535fca8963611fafbc.zip
Settings: Initial
Change-Id: If8199313561dfba03a1aad0b49bb4036dcf586c9
Diffstat (limited to 'src/com/android/launcher3/settings')
-rw-r--r--src/com/android/launcher3/settings/SettingsActivity.java225
-rw-r--r--src/com/android/launcher3/settings/SettingsProvider.java50
2 files changed, 275 insertions, 0 deletions
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;
+ }
+}