summaryrefslogtreecommitdiffstats
path: root/src/org/codeaurora/gallery3d/video/SettingsActivity.java
blob: 3d7fac5733e696099f401ba5a3c086d49c162bcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package org.codeaurora.gallery3d.video;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemProperties;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.RingtonePreference;
import android.preference.PreferenceScreen;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.provider.Settings.System;
import android.provider.Telephony;
import android.telephony.TelephonyManager;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import com.android.gallery3d.R;

import java.util.ArrayList;

public class SettingsActivity extends PreferenceActivity {

    private static final String LOG_TAG = "SettingsActivity";

    public  static final String PREFERENCE_RTP_MINPORT = "rtp_min_port";
    public  static final String PREFERENCE_RTP_MAXPORT = "rtp_max_port";
    private static final String PREFERENCE_KEEP_ALIVE_INTERVAL_SECOND = "keep_alive_interval_second";
    private static final String PREFERENCE_CACHE_MIN_SIZE = "cache_min_size";
    private static final String PREFERENCE_CACHE_MAX_SIZE = "cache_max_size";
    public  static final String PREFERENCE_BUFFER_SIZE = "buffer_size";
    public  static final String PREFERENCE_APN = "apn";
    private static final String PACKAGE_NAME  = "com.android.settings";

    private static final int DEFAULT_RTP_MINPORT = 8192;
    private static final int DEFAULT_RTP_MAXPORT = 65535;
    private static final int DEFAULT_CACHE_MIN_SIZE = 4 * 1024 * 1024;
    private static final int DEFAULT_CACHE_MAX_SIZE = 20 * 1024 * 1024;
    private static final int DEFAULT_KEEP_ALIVE_INTERVAL_SECOND = 15;
    
    private static final int RTP_MIN_PORT = 1;
    private static final int RTP_MAX_PORT = 2;
    private static final int BUFFER_SIZE  = 3;
    
    private SharedPreferences  mPref;
    private EditTextPreference mRtpMinPort;
    private EditTextPreference mRtpMaxPort;
    private EditTextPreference mBufferSize;
    private PreferenceScreen   mApn;
    
    private static final int    SELECT_APN = 1;
    public  static final String PREFERRED_APN_URI = "content://telephony/carriers/preferapn";
    private static final Uri    PREFERAPN_URI = Uri.parse(PREFERRED_APN_URI);
    private static final int    COLUMN_ID_INDEX = 0;
    private static final int    NAME_INDEX = 1;
    private static final String RTP_PORTS_PROPERTY_NAME = "persist.env.media.rtp-ports";
    private static final String CACHE_PROPERTY_NAME = "persist.env.media.cache-params";

    private boolean mUseNvOperatorForEhrpd = SystemProperties.getBoolean(
            "persist.radio.use_nv_for_ehrpd", false);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.rtsp_settings_preferences);

        mPref = getPreferenceScreen().getSharedPreferences();
        mRtpMinPort = (EditTextPreference) findPreference(PREFERENCE_RTP_MINPORT);
        mRtpMaxPort = (EditTextPreference) findPreference(PREFERENCE_RTP_MAXPORT);
        mBufferSize = (EditTextPreference) findPreference(PREFERENCE_BUFFER_SIZE);
        mApn = (PreferenceScreen) findPreference(PREFERENCE_APN);
        
        setPreferenceListener(RTP_MIN_PORT, mRtpMinPort);
        setPreferenceListener(RTP_MAX_PORT, mRtpMaxPort);
        setPreferenceListener(BUFFER_SIZE, mBufferSize);
        setApnListener();

        ActionBar ab = getActionBar();
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setTitle(R.string.setting);
    }
    
    private String getApnKey() {
        // to find default key
        String key = null;
        Cursor cursor = getContentResolver().query(PREFERAPN_URI, new String[] {
                "_id"
        }, null, null, Telephony.Carriers.DEFAULT_SORT_ORDER);
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
            key = cursor.getString(COLUMN_ID_INDEX);
            Log.v("settingActivty", "default apn key = " + key);
        }
        cursor.close();
        return key;
    }
    
    private String getApnName(String key) {
        String name = null;
        // to find default proxy
        String where = getOperatorNumericSelection();

        Cursor cursor = getContentResolver().query(Telephony.Carriers.CONTENT_URI, new String[] {
                "_id", "name", "apn", "type"
        }, where, null, Telephony.Carriers.DEFAULT_SORT_ORDER);

        while (cursor != null && cursor.moveToNext()) {
            String curKey = cursor.getString(cursor.getColumnIndex("_id"));
            String curName = cursor.getString(cursor.getColumnIndex("name"));
            if (curKey.equals(key)) {
                Log.d("rtsp", "getDefaultApnName, find, key=" + curKey + ",curName=" + curName);
                name = curName;
                break;
            }
        }

        if (cursor != null) {
            cursor.close();
        }
        return name;
        
    }

    private String getDefaultApnName() {
        return getApnName(getApnKey());
    }

    private String getSelectedApnKey() {
        String key = null;

        Cursor cursor = getContentResolver().query(PREFERAPN_URI, new String[] {
                "_id", "name"
        }, null, null, null);
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            key = cursor.getString(NAME_INDEX);
        }
        cursor.close();

        Log.w("rtsp", "getSelectedApnKey key = " + key);
        if (null == key)
            return new String("null");
        return key;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SELECT_APN) {
            setResult(resultCode);
            finish();
            Log.w(LOG_TAG, "onActivityResult requestCode = " + requestCode);
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    private String getOperatorNumericSelection() {
        String[] mccmncs = getOperatorNumeric();
        String where;
        where = (mccmncs[0] != null) ? "numeric=\"" + mccmncs[0] + "\"" : "";
        where += (mccmncs[1] != null) ? " or numeric=\"" + mccmncs[1] + "\"" : "";
        Log.d(LOG_TAG, "getOperatorNumericSelection: " + where);
        return where;
    }

    private String[] getOperatorNumeric() {
        ArrayList<String> result = new ArrayList<String>();
        String mccMncFromSim = null;
        if (mUseNvOperatorForEhrpd) {
            String mccMncForEhrpd = SystemProperties.get("ro.cdma.home.operator.numeric", null);
            if (mccMncForEhrpd != null && mccMncForEhrpd.length() > 0) {
                result.add(mccMncForEhrpd);
            }
        }

        mccMncFromSim = TelephonyManager.getDefault().getSimOperator();

        if (mccMncFromSim != null && mccMncFromSim.length() > 0) {
            result.add(mccMncFromSim);
        }
        return result.toArray(new String[2]);
    }

    private void enableRtpPortSetting() {
        final String rtpMinPortStr = mPref.getString(PREFERENCE_RTP_MINPORT, 
                Integer.toString(DEFAULT_RTP_MINPORT));
        final String rtpMaxPortStr = mPref.getString(PREFERENCE_RTP_MAXPORT,
                Integer.toString(DEFAULT_RTP_MAXPORT));
        // System property format: "rtpMinPort/rtpMaxPort"
        final String propertyValue = rtpMinPortStr + "/" + rtpMaxPortStr;
        Log.v(LOG_TAG, "set system property " + RTP_PORTS_PROPERTY_NAME + " : " + propertyValue);
        SystemProperties.set(RTP_PORTS_PROPERTY_NAME, propertyValue);
    }

    private void enableBufferSetting() {
        final String bufferSizeStr = mPref.getString(PREFERENCE_BUFFER_SIZE, 
                Integer.toString(DEFAULT_CACHE_MAX_SIZE));
        final int cacheMaxSize;
        final String ACTION_NAME = "org.codeaurora.gallery3d.video.STREAMING_SETTINGS_ENABLER";
        try {
            cacheMaxSize = Integer.valueOf(bufferSizeStr);
        } catch (NumberFormatException e) {
            Log.e(LOG_TAG, "Failed to parse cache max size");
            return;
        }
        // System property format: "minCacheSizeKB/maxCacheSizeKB/keepAliveIntervalSeconds"
        final String propertyValue = (DEFAULT_CACHE_MIN_SIZE / 1024) + "/" +
                (cacheMaxSize / 1024) + "/" + DEFAULT_KEEP_ALIVE_INTERVAL_SECOND;
        Log.v(LOG_TAG, "set system property " + CACHE_PROPERTY_NAME + " : " + propertyValue);
        SystemProperties.set(CACHE_PROPERTY_NAME, propertyValue);
    }
    
    private void setPreferenceListener(final int which, final EditTextPreference etp) {

        final String DIGITS_ACCEPTABLE = "0123456789";
        String summaryStr = "";
        String preferStr  = "";

        switch (which) {
            case RTP_MIN_PORT:
                preferStr = mPref.getString(PREFERENCE_RTP_MINPORT,
                        Integer.toString(DEFAULT_RTP_MINPORT));
                summaryStr = "streaming_min_udp_port";
                break;
            case RTP_MAX_PORT:
                preferStr = mPref.getString(PREFERENCE_RTP_MAXPORT, 
                        Integer.toString(DEFAULT_RTP_MAXPORT));
                summaryStr = "streaming_max_udp_port";
                break;
            case BUFFER_SIZE:
                preferStr = mPref.getString(PREFERENCE_BUFFER_SIZE, 
                        Integer.toString(DEFAULT_CACHE_MAX_SIZE));
                break;
            default:
                return;
            
        }

        final String summaryString = summaryStr; 
        etp.getEditText().setKeyListener(DigitsKeyListener.getInstance(DIGITS_ACCEPTABLE));
        etp.setSummary(preferStr);
        etp.setText(preferStr);
        etp.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                final String summary = newValue.toString();
                final int value;
                try {
                    value = Integer.valueOf(summary);
                } catch (NumberFormatException e) {
                    Log.e(LOG_TAG, "NumberFormatException");
                    return false;
                }
                etp.setSummary(summary);
                etp.setText(summary);
                Log.d(LOG_TAG, "z66/z82 summary = " + summary);
                if(which == RTP_MIN_PORT || which == RTP_MAX_PORT) {
                    System.putString(getContentResolver(), summaryString, summary);
                    enableRtpPortSetting();
                } else {
                    enableBufferSetting();
                }
                return true;
            }
        });

    }
    
    private void setApnListener() {
        final String SUBSCRIPTION_KEY = "subscription";
        mApn.setSummary(getDefaultApnName());
        mApn.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                Intent intent = new Intent(Settings.ACTION_APN_SETTINGS);
                int subscription = 0;
                try {
                    subscription = Settings.Global.getInt(SettingsActivity.this.getContentResolver(),
                            Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION);
                } catch (Exception e) {
                    Log.d("SettingActivity", "Can't get subscription for Exception: " + e);
                } finally {
                    intent.putExtra(SUBSCRIPTION_KEY, subscription);
                }
                startActivityForResult(intent, SELECT_APN);
                return true;
            }
        });
    }
}