1 /* 2 * Copyright (C) 2007-2008 Esmertec AG. 3 * Copyright (C) 2007-2008 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.im.app; 19 20 import java.util.HashMap; 21 22 import android.app.Activity; 23 import android.content.ContentValues; 24 import android.content.Intent; 25 import android.database.Cursor; 26 import android.os.Bundle; 27 import android.provider.Im; 28 import android.util.Log; 29 import android.view.View; 30 import android.widget.Button; 31 import android.widget.EditText; 32 import android.widget.RadioGroup; 33 34 import com.android.im.R; 35 import com.android.im.imps.ImpsConnectionConfig.CirMethod; 36 import com.android.im.imps.ImpsConnectionConfig.EncodingType; 37 import com.android.im.imps.ImpsConnectionConfig.TransportType; 38 import com.android.im.plugin.ImConfigNames; 39 import com.android.im.plugin.ImpsConfigNames; 40 41 public class PreferenceActivity extends Activity { 42 43 RadioGroup mRgDataChannel; 44 RadioGroup mRgDataEncoding; 45 RadioGroup mRgCirChannel; 46 47 EditText mEdtHost; 48 EditText mEdtMsisdn; 49 50 long mProviderId; 51 String mProviderName; 52 HashMap<String, String> mPref; 53 log(String log)54 static final void log(String log) { 55 Log.d(ImApp.LOG_TAG, "<PreferenceActivity> " + log); 56 } 57 58 @Override onCreate(Bundle icicle)59 public void onCreate(Bundle icicle) { 60 super.onCreate(icicle); 61 62 resolveIntent(); 63 setTitle(getString(R.string.preference_title, mProviderName)); 64 setContentView(R.layout.preference_activity); 65 66 mRgDataChannel = (RadioGroup) findViewById(R.id.rgDataChannel); 67 mRgDataEncoding = (RadioGroup) findViewById(R.id.rgDataEncoding); 68 mRgCirChannel = (RadioGroup) findViewById(R.id.rgCirChannel); 69 mEdtHost = (EditText) findViewById(R.id.etHost); 70 mEdtMsisdn = (EditText) findViewById(R.id.etMsisdn); 71 72 String dataChannel = getPreference(ImpsConfigNames.DATA_CHANNEL, 73 TransportType.HTTP.name()); 74 if (TransportType.HTTP.name().equals(dataChannel)) { 75 mRgDataChannel.check(R.id.DATA_HTTP); 76 } else if (TransportType.SMS.name().equals(dataChannel)) { 77 mRgDataChannel.check(R.id.DATA_SMS); 78 } 79 80 String cirChannel = getPreference(ImpsConfigNames.CIR_CHANNEL, 81 CirMethod.STCP.name()); 82 if (CirMethod.STCP.name().equals(cirChannel)) { 83 mRgCirChannel.check(R.id.CIR_STCP); 84 } else if (CirMethod.SHTTP.name().equals(cirChannel)) { 85 mRgCirChannel.check(R.id.CIR_SHTTP); 86 } else if (CirMethod.SSMS.name().equals(cirChannel)) { 87 mRgCirChannel.check(R.id.CIR_SSMS); 88 } 89 90 String dataEncoding = getPreference(ImpsConfigNames.DATA_ENCODING, 91 EncodingType.XML.name()); 92 if (EncodingType.XML.name().equals(dataEncoding)) { 93 mRgDataEncoding.check(R.id.ENC_XML); 94 } else if (EncodingType.WBXML.name().equals(dataEncoding)) { 95 mRgDataEncoding.check(R.id.ENC_WBXML); 96 } else if (EncodingType.SMS.name().equals(dataEncoding)) { 97 mRgDataEncoding.check(R.id.ENC_SMS); 98 } 99 100 mEdtHost.setText(getPreference(ImpsConfigNames.HOST, "http://")); 101 mEdtMsisdn.setText(getPreference(ImpsConfigNames.MSISDN, "")); 102 103 final Button btnSave = (Button) findViewById(R.id.btnSave); 104 btnSave.setOnClickListener(new Button.OnClickListener() { 105 public void onClick(View v) { 106 savePreferences(); 107 } 108 }); 109 } 110 getPreference(String prefName, String defaultValue)111 private String getPreference(String prefName, String defaultValue) { 112 String value = mPref.get(prefName); 113 114 return value == null ? defaultValue : value; 115 } 116 resolveIntent()117 void resolveIntent() { 118 Intent i = getIntent(); 119 if(i.getData() == null){ 120 Log.w(ImApp.LOG_TAG, "No data passed to PreferenceActivity"); 121 finish(); 122 } else { 123 Cursor c = getContentResolver().query(i.getData(), 124 new String[]{Im.Provider._ID, Im.Provider.NAME}, null, null, null); 125 if (c == null || !c.moveToFirst()) { 126 Log.w(ImApp.LOG_TAG, "Can't query data from given URI."); 127 finish(); 128 } else { 129 mProviderId = c.getLong(0); 130 mProviderName = c.getString(1); 131 132 c.close(); 133 134 mPref = Im.ProviderSettings.queryProviderSettings(getContentResolver(), mProviderId); 135 } 136 } 137 } 138 savePreferences()139 void savePreferences() { 140 TransportType dataChannel; 141 switch (mRgDataChannel.getCheckedRadioButtonId()) { 142 case R.id.DATA_HTTP: 143 dataChannel = TransportType.HTTP; 144 break; 145 case R.id.DATA_SMS: 146 dataChannel = TransportType.SMS; 147 break; 148 default: 149 Log.w(ImApp.LOG_TAG, "Unexpected dataChannel button ID; defaulting to HTTP"); 150 dataChannel = TransportType.HTTP; 151 break; 152 } 153 154 CirMethod cirChannel; 155 switch (mRgCirChannel.getCheckedRadioButtonId()) { 156 case R.id.CIR_STCP: 157 cirChannel = CirMethod.STCP; 158 break; 159 case R.id.CIR_SHTTP: 160 cirChannel = CirMethod.SHTTP; 161 break; 162 case R.id.CIR_SSMS: 163 cirChannel = CirMethod.SSMS; 164 break; 165 default: 166 Log.w(ImApp.LOG_TAG, "Unexpected cirChannel button ID; defaulting to STCP"); 167 cirChannel = CirMethod.STCP; 168 break; 169 } 170 171 EncodingType dataEncoding; 172 switch (mRgDataEncoding.getCheckedRadioButtonId()) { 173 case R.id.ENC_WBXML: 174 dataEncoding = EncodingType.WBXML; 175 break; 176 case R.id.ENC_XML: 177 dataEncoding = EncodingType.XML; 178 break; 179 case R.id.ENC_SMS: 180 dataEncoding = EncodingType.SMS; 181 break; 182 default: 183 Log.w(ImApp.LOG_TAG, "Unexpected dataEncoding button ID; defaulting to WBXML"); 184 dataEncoding = EncodingType.WBXML; 185 break; 186 } 187 188 String host = mEdtHost.getText().toString(); 189 String msisdn = mEdtMsisdn.getText().toString(); 190 191 if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){ 192 log("set connection preference, DataChannel: " + dataChannel 193 + ", CirChannel: " + cirChannel 194 + ", DataEncoding: " + dataEncoding 195 + ", Host: " + host 196 + ", MSISDN: " + msisdn); 197 } 198 ContentValues[] valuesList = new ContentValues[7]; 199 valuesList[0] = getValues(ImConfigNames.PROTOCOL_NAME, "IMPS"); 200 valuesList[1] = getValues(ImpsConfigNames.DATA_CHANNEL, dataChannel.name()); 201 valuesList[2] = getValues(ImpsConfigNames.DATA_ENCODING, dataEncoding.name()); 202 valuesList[3] = getValues(ImpsConfigNames.CIR_CHANNEL, cirChannel.name()); 203 valuesList[4] = getValues(ImpsConfigNames.HOST, host); 204 valuesList[6] = getValues(ImpsConfigNames.MSISDN, msisdn); 205 206 getContentResolver().bulkInsert(Im.ProviderSettings.CONTENT_URI, valuesList); 207 208 finish(); 209 } 210 getValues(String name, String value)211 private ContentValues getValues(String name, String value) { 212 ContentValues values = new ContentValues(); 213 values.put(Im.ProviderSettings.PROVIDER, mProviderId); 214 values.put(Im.ProviderSettings.NAME, name); 215 values.put(Im.ProviderSettings.VALUE, value); 216 217 return values; 218 } 219 } 220