SettingsActivity.java 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2012-2013 NetEase, Inc. and other contributors
 *
 *  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.
 *
 */
K
kevinkong 已提交
17 18
package com.netease.qa.emmagee.activity;

19 20
import java.io.DataOutputStream;

21 22 23
import com.netease.qa.emmagee.R;
import com.netease.qa.emmagee.utils.Settings;

K
kevinkong 已提交
24 25
import android.app.Activity;
import android.content.Intent;
26
import android.content.SharedPreferences;
K
kevinkong 已提交
27 28 29 30
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
31
import android.view.Window;
K
kevinkong 已提交
32
import android.widget.CheckBox;
33 34
import android.widget.ImageView;
import android.widget.LinearLayout;
35
import android.widget.RelativeLayout;
36 37 38
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
39
import android.widget.Toast;
K
kevinkong 已提交
40

41 42
/**
 * Setting Page of Emmagee
A
andrewleo2013 已提交
43
 * 
A
andrewleo 已提交
44
 * @author andrewleo
45
 */
K
kevinkong 已提交
46 47
public class SettingsActivity extends Activity {

48
	private static final String LOG_TAG = "Emmagee-" + SettingsActivity.class.getSimpleName();
A
andrewleo2013 已提交
49

K
kevinkong 已提交
50
	private CheckBox chkFloat;
51
	private CheckBox chkRoot;
52
	private CheckBox chkAutoStop;
53
	private TextView tvTime;
54 55
	private LinearLayout about;
	private LinearLayout mailSettings;
K
kevinkong 已提交
56

57
	private SharedPreferences preferences;
58

K
kevinkong 已提交
59 60 61 62
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i(LOG_TAG, "onCreate");
		super.onCreate(savedInstanceState);
63
		requestWindowFeature(Window.FEATURE_NO_TITLE);
K
kevinkong 已提交
64 65 66
		setContentView(R.layout.settings);

		chkFloat = (CheckBox) findViewById(R.id.floating);
67
		chkRoot = (CheckBox) findViewById(R.id.is_root);
68
		chkAutoStop = (CheckBox) findViewById(R.id.auto_stop);
69
		tvTime = (TextView) findViewById(R.id.time);
70 71
		about = (LinearLayout) findViewById(R.id.about);
		mailSettings = (LinearLayout) findViewById(R.id.mail_settings);
72
		SeekBar timeBar = (SeekBar) findViewById(R.id.timeline);
73
		ImageView btnSave = (ImageView) findViewById(R.id.btn_set);
黄庆兵 已提交
74
		RelativeLayout floatingItem = (RelativeLayout) findViewById(R.id.floating_item);
75
		RelativeLayout autoStopItem = (RelativeLayout) findViewById(R.id.auto_stop_item);
76
		LinearLayout layGoBack = (LinearLayout) findViewById(R.id.lay_go_back);
77
		LinearLayout layHeapItem = (LinearLayout) findViewById(R.id.heap_item);
78
		
黄庆兵 已提交
79

80
		btnSave.setVisibility(ImageView.INVISIBLE);
81
		
82 83 84
		preferences = Settings.getDefaultSharedPreferences(getApplicationContext());
		int interval = preferences.getInt(Settings.KEY_INTERVAL, 5);
		boolean isfloat = preferences.getBoolean(Settings.KEY_ISFLOAT, true);
85
		boolean isRoot = preferences.getBoolean(Settings.KEY_ROOT, false);
86 87
		boolean autoStop = preferences.getBoolean(Settings.KEY_AUTO_STOP, true);
		
88 89
		tvTime.setText(String.valueOf(interval));
		chkFloat.setChecked(isfloat);
90
		chkRoot.setChecked(isRoot);
91
		chkAutoStop.setChecked(autoStop);
92
		
93
		timeBar.setProgress(interval);
94 95 96 97 98 99 100 101 102 103 104 105
		timeBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				tvTime.setText(Integer.toString(arg1 + 1));
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
106
				// when tracking stoped, update preferences
107
				int interval = arg0.getProgress() + 1;
108
				preferences.edit().putInt(Settings.KEY_INTERVAL, interval).commit();
109 110
			}
		});
黄庆兵 已提交
111

112
		layGoBack.setOnClickListener(new OnClickListener() {
113 114 115 116
			@Override
			public void onClick(View arg0) {
				SettingsActivity.this.finish();
				Intent intent = new Intent();
117
				intent.setClass(SettingsActivity.this, MainPageActivity.class);
118 119 120
				startActivity(intent);
			}
		});
121

122 123 124 125
		mailSettings.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
126 127
				intent.setClass(SettingsActivity.this, MailSettingsActivity.class);
				startActivity(intent);
128 129 130 131 132 133 134 135
			}
		});

		about.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setClass(SettingsActivity.this, AboutActivity.class);
136
				startActivity(intent);
137 138 139
			}
		});

黄庆兵 已提交
140
		floatingItem.setOnClickListener(new OnClickListener() {
141 142
			@Override
			public void onClick(View arg0) {
143 144 145 146 147
				boolean isChecked = chkFloat.isChecked();
				chkFloat.setChecked(!isChecked);
				preferences.edit().putBoolean(Settings.KEY_ISFLOAT, !isChecked).commit();
			}
		});
148 149 150 151 152 153 154 155 156 157
		
		autoStopItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				boolean isChecked = chkAutoStop.isChecked();
				chkAutoStop.setChecked(!isChecked);
				preferences.edit().putBoolean(Settings.KEY_AUTO_STOP, !isChecked).commit();
			}
		});
		
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
		// get root permission
		layHeapItem.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// if root checkbox is checked, change status to
				// opposite;otherwise, try to upgrade app to root
				boolean isChecked = chkRoot.isChecked();
				if (isChecked) {
					chkRoot.setChecked(!isChecked);
					preferences.edit().putBoolean(Settings.KEY_ROOT, !isChecked).commit();
				} else {
					boolean root = upgradeRootPermission(getPackageCodePath());
					if (root) {
						Log.d(LOG_TAG, "root succeed");
						chkRoot.setChecked(!isChecked);
						preferences.edit().putBoolean(Settings.KEY_ROOT, !isChecked).commit();
					} else {
						// if root failed, tell user to check if phone is rooted
						Toast.makeText(getBaseContext(), getString(R.string.root_failed_notification), Toast.LENGTH_LONG).show();
					}
				}

180 181
			}
		});
K
kevinkong 已提交
182 183 184 185 186 187 188 189 190 191 192 193
	}

	@Override
	public void finish() {
		super.finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

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
	/**
	 * upgrade app to get root permission
	 * 
	 * @return is root successfully
	 */
	public static boolean upgradeRootPermission(String pkgCodePath) {
		Process process = null;
		DataOutputStream os = null;
		try {
			String cmd = "chmod 777 " + pkgCodePath;
			process = Runtime.getRuntime().exec("su"); // 切换到root帐号
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(cmd + "\n");
			os.writeBytes("exit\n");
			os.flush();
			int existValue = process.waitFor();
			if (existValue == 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			Log.w(LOG_TAG, "upgradeRootPermission exception=" + e.getMessage());
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
			}
		}
	}
K
kevinkong 已提交
228
}