1 /* 2 * Copyright (C) 2015 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.android.messaging.ui.appsettings; 18 19 import android.app.Fragment; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.support.v4.app.NavUtils; 23 import android.text.TextUtils; 24 import android.view.LayoutInflater; 25 import android.view.MenuItem; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.view.ViewGroup; 29 import android.widget.ArrayAdapter; 30 import android.widget.ListView; 31 import android.widget.TextView; 32 33 import com.android.messaging.R; 34 import com.android.messaging.datamodel.DataModel; 35 import com.android.messaging.datamodel.binding.Binding; 36 import com.android.messaging.datamodel.binding.BindingBase; 37 import com.android.messaging.datamodel.data.SettingsData; 38 import com.android.messaging.datamodel.data.SettingsData.SettingsDataListener; 39 import com.android.messaging.datamodel.data.SettingsData.SettingsItem; 40 import com.android.messaging.ui.BugleActionBarActivity; 41 import com.android.messaging.ui.UIIntents; 42 import com.android.messaging.util.Assert; 43 import com.android.messaging.util.PhoneUtils; 44 45 import java.util.ArrayList; 46 import java.util.List; 47 48 /** 49 * Shows the "master" settings activity that contains two parts, one for application-wide settings 50 * (dubbed "General settings"), and one or more for per-subscription settings (dubbed "Messaging 51 * settings" for single-SIM, and the actual SIM name for multi-SIM). Clicking on either item 52 * (e.g. "General settings") will open the detail settings activity (ApplicationSettingsActivity 53 * in this case). 54 */ 55 public class SettingsActivity extends BugleActionBarActivity { 56 @Override 57 protected void onCreate(final Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 60 61 // Directly open the detailed settings page as the top-level settings activity if this is 62 // not a multi-SIM device. 63 if (PhoneUtils.getDefault().getActiveSubscriptionCount() <= 1) { 64 UIIntents.get().launchApplicationSettingsActivity(this, true /* topLevel */); 65 finish(); 66 } else { 67 getFragmentManager().beginTransaction() 68 .replace(android.R.id.content, new SettingsFragment()) 69 .commit(); 70 } 71 } 72 73 @Override 74 public boolean onOptionsItemSelected(final MenuItem item) { 75 switch (item.getItemId()) { 76 case android.R.id.home: 77 NavUtils.navigateUpFromSameTask(this); 78 return true; 79 } 80 return super.onOptionsItemSelected(item); 81 } 82 83 public static class SettingsFragment extends Fragment implements SettingsDataListener { 84 private ListView mListView; 85 private SettingsListAdapter mAdapter; 86 private final Binding<SettingsData> mBinding = BindingBase.createBinding(this); 87 88 @Override 89 public void onCreate(final Bundle savedInstanceState) { 90 super.onCreate(savedInstanceState); 91 mBinding.bind(DataModel.get().createSettingsData(getActivity(), this)); 92 mBinding.getData().init(getLoaderManager(), mBinding); 93 } 94 95 @Override 96 public View onCreateView(final LayoutInflater inflater, final ViewGroup container, 97 final Bundle savedInstanceState) { 98 final View view = inflater.inflate(R.layout.settings_fragment, container, false); 99 mListView = (ListView) view.findViewById(android.R.id.list); 100 mAdapter = new SettingsListAdapter(getActivity()); 101 mListView.setAdapter(mAdapter); 102 return view; 103 } 104 105 @Override 106 public void onDestroy() { 107 super.onDestroy(); 108 mBinding.unbind(); 109 } 110 111 @Override 112 public void onSelfParticipantDataLoaded(SettingsData data) { 113 mBinding.ensureBound(data); 114 mAdapter.setSettingsItems(data.getSettingsItems()); 115 } 116 117 /** 118 * An adapter that displays a list of SettingsItem. 119 */ 120 private class SettingsListAdapter extends ArrayAdapter<SettingsItem> { 121 public SettingsListAdapter(final Context context) { 122 super(context, R.layout.settings_item_view, new ArrayList<SettingsItem>()); 123 } 124 125 public void setSettingsItems(final List<SettingsItem> newList) { 126 clear(); 127 addAll(newList); 128 notifyDataSetChanged(); 129 } 130 131 @Override 132 public View getView(final int position, final View convertView, 133 final ViewGroup parent) { 134 View itemView; 135 if (convertView != null) { 136 itemView = convertView; 137 } else { 138 final LayoutInflater inflater = (LayoutInflater) getContext() 139 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 140 itemView = inflater.inflate( 141 R.layout.settings_item_view, parent, false); 142 } 143 final SettingsItem item = getItem(position); 144 final TextView titleTextView = (TextView) itemView.findViewById(R.id.title); 145 final TextView subtitleTextView = (TextView) itemView.findViewById(R.id.subtitle); 146 final String summaryText = item.getDisplayDetail(); 147 titleTextView.setText(item.getDisplayName()); 148 if (!TextUtils.isEmpty(summaryText)) { 149 subtitleTextView.setText(summaryText); 150 subtitleTextView.setVisibility(View.VISIBLE); 151 } else { 152 subtitleTextView.setVisibility(View.GONE); 153 } 154 itemView.setOnClickListener(new OnClickListener() { 155 @Override 156 public void onClick(View view) { 157 switch (item.getType()) { 158 case SettingsItem.TYPE_GENERAL_SETTINGS: 159 UIIntents.get().launchApplicationSettingsActivity(getActivity(), 160 false /* topLevel */); 161 break; 162 163 case SettingsItem.TYPE_PER_SUBSCRIPTION_SETTINGS: 164 UIIntents.get().launchPerSubscriptionSettingsActivity(getActivity(), 165 item.getSubId(), item.getActivityTitle()); 166 break; 167 168 default: 169 Assert.fail("unrecognized setting type!"); 170 break; 171 } 172 } 173 }); 174 return itemView; 175 } 176 } 177 } 178 } 179