MainPageActivity.java 9.3 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 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
package com.netease.qa.emmagee.activity;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.netease.qa.emmagee.service.EmmageeService;
import com.netease.qa.emmagee.utils.ProcessInfo;
import com.netease.qa.emmagee.utils.Programe;
import com.netease.qa.emmagee.R;

public class MainPageActivity extends Activity {

	private final String LOG_TAG = "Emmagee-"
			+ MainPageActivity.class.getSimpleName();

A
andrewleo 已提交
61 62
	private final int TIMEOUT = 20000;
	
K
kevinkong 已提交
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
	private List<Programe> processList;
	private ProcessInfo processInfo;
	private Intent MonitorService;
	private ListView lstViProgramme;
	private Button btnTest;
	private boolean isTesting = true;
	private boolean isRadioChecked = false;
	private int pid, uid;
	private String processName, packageName, settingTempFile;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.i(LOG_TAG, "MainActivity::onCreate");
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.mainpage);
		createNewFile();
		processInfo = new ProcessInfo();
		lstViProgramme = (ListView) findViewById(R.id.processList);
		btnTest = (Button) findViewById(R.id.test);
		lstViProgramme.setAdapter(new ListAdapter());
		btnTest.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MonitorService = new Intent();
A
andrewleo 已提交
90 91
				MonitorService.setClass(MainPageActivity.this,
						EmmageeService.class);
K
kevinkong 已提交
92 93 94 95
				if (isTesting) {
					if (isRadioChecked == true) {
						Intent intent = getPackageManager()
								.getLaunchIntentForPackage(packageName);
96
						Log.d(LOG_TAG, packageName);
K
kevinkong 已提交
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
						startActivity(intent);
						waitForAppStart(packageName);
						MonitorService.putExtra("processName", processName);
						MonitorService.putExtra("pid", pid);
						MonitorService.putExtra("uid", uid);
						MonitorService.putExtra("packageName", packageName);
						MonitorService.putExtra("settingTempFile",
								settingTempFile);
						startService(MonitorService);
						btnTest.setText("停止测试");
						isTesting = false;
					} else {
						Toast.makeText(MainPageActivity.this, "请选择需要测试的应用程序",
								Toast.LENGTH_LONG).show();
					}
				} else {
					btnTest.setText("开始测试");
					isTesting = true;
					Toast.makeText(MainPageActivity.this,
							"测试结果文件:" + EmmageeService.resultFilePath,
							Toast.LENGTH_LONG).show();
					stopService(MonitorService);
				}
			}
		});
	}

	/**
	 * create new file to reserve setting data
	 */
	private void createNewFile() {
		Log.i(LOG_TAG, "create new file to save setting data");
		settingTempFile = getBaseContext().getFilesDir().getPath()
				+ "\\Emmagee_Settings.txt";
		File settingFile = new File(settingTempFile);
A
andrewleo 已提交
132
		if (!settingFile.exists()) 
K
kevinkong 已提交
133 134 135 136 137 138 139 140 141 142 143 144
			try {
				settingFile.createNewFile();
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(settingFile)));
				bw.write("5" + "\r\n" + "true");
				bw.close();
			} catch (IOException e) {
				Log.d(LOG_TAG, "create new file exception :" + e.getMessage());
			}
	}

	/**
145
	 * wait for test application started , timeout is 20s
K
kevinkong 已提交
146 147
	 * 
	 * @param packageName
148
	 *             package name of test application
K
kevinkong 已提交
149 150 151
	 */
	private void waitForAppStart(String packageName) {
		Log.d(LOG_TAG, "wait for app start");
A
andrewleo 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165
		boolean isProcessStarted = false;
		long startTime = System.currentTimeMillis();
		while (System.currentTimeMillis() < startTime + TIMEOUT) {
			processList = processInfo.getRunningProcess(getBaseContext());
			for (Programe programe : processList) {
				if ((programe.getPackageName() != null)
						&& (programe.getPackageName().equals(packageName))) {
					pid = programe.getPid();
					Log.d(LOG_TAG, "pid:" + pid);
					uid = programe.getUid();
					if (pid != 0) {
						isProcessStarted = true;
						break;
					}
K
kevinkong 已提交
166 167
				}
			}
A
andrewleo 已提交
168 169
			if (isProcessStarted)
				break;
K
kevinkong 已提交
170 171 172 173
		}
	}

	/**
174
	 * override return key to show a dialog 
K
kevinkong 已提交
175 176 177 178 179 180 181 182 183
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			showDialog(0);
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
A
andrewleo2013 已提交
184
	 * set menu options
K
kevinkong 已提交
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
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0, "退出").setIcon(
				android.R.drawable.ic_menu_delete);
		menu.add(0, Menu.FIRST, 1, "设置").setIcon(
				android.R.drawable.ic_menu_directions);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getOrder()) {
		case 0:
			showDialog(0);
			break;
		case 1:
			Intent intent = new Intent();
			intent.setClass(MainPageActivity.this, SettingsActivity.class);
			intent.putExtra("settingTempFile", settingTempFile);
			startActivityForResult(intent, Activity.RESULT_FIRST_USER);
			break;
		default:
			break;
		}
		return false;
	}

	/**
A
andrewleo2013 已提交
212
	 * create a dialog
K
kevinkong 已提交
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
	 */
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 0:
			return new AlertDialog.Builder(this)
					.setTitle("确定退出程序?")
					.setPositiveButton(
							"确定",
							new android.content.DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									if (MonitorService != null) {
										Log.d(LOG_TAG, "stop service");
										stopService(MonitorService);
									}
									Log.d(LOG_TAG, "exit Emmagee");
									EmmageeService.closeOpenedStream();
									finish();
									System.exit(0);
								}
							}).setNegativeButton("取消", null).create();
		default:
			return null;
		}
	}

	/**
241
	 * customizing adapter
K
kevinkong 已提交
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 309 310 311 312 313 314 315 316 317
	 * 
	 */
	private class ListAdapter extends BaseAdapter {
		List<Programe> programe;
		int tempPosition = -1;

		class Viewholder {
			TextView txtAppName;
			ImageView imgViAppIcon;
			RadioButton rdoBtnApp;
		}

		public ListAdapter() {
			programe = processInfo.getRunningProcess(getBaseContext());
		}

		@Override
		public int getCount() {
			return programe.size();
		}

		@Override
		public Object getItem(int position) {
			return programe.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Viewholder holder = new Viewholder();
			final int i = position;
			convertView = MainPageActivity.this.getLayoutInflater().inflate(
					R.layout.list_item, null);
			holder.imgViAppIcon = (ImageView) convertView
					.findViewById(R.id.image);
			holder.txtAppName = (TextView) convertView.findViewById(R.id.text);
			holder.rdoBtnApp = (RadioButton) convertView.findViewById(R.id.rb);
			holder.rdoBtnApp.setId(position);
			holder.rdoBtnApp
					.setOnCheckedChangeListener(new OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							if (isChecked) {
								isRadioChecked = true;
								// Radio function
								if (tempPosition != -1) {
									RadioButton tempButton = (RadioButton) findViewById(tempPosition);
									if ((tempButton != null)
											&& (tempPosition != i)) {
										tempButton.setChecked(false);
									}
								}

								tempPosition = buttonView.getId();
								packageName = programe.get(tempPosition)
										.getPackageName();
								processName = programe.get(tempPosition)
										.getProcessName();
							}
						}
					});
			if (tempPosition == position) {
				if (!holder.rdoBtnApp.isChecked())
					holder.rdoBtnApp.setChecked(true);
			}
			Programe pr = (Programe) programe.get(position);
			holder.imgViAppIcon.setImageDrawable(pr.getIcon());
			holder.txtAppName.setText(pr.getProcessName());
			return convertView;
		}
	}
A
andrewleo 已提交
318

K
kevinkong 已提交
319 320 321 322 323 324 325 326 327 328
	@Override
	public void finish() {
		super.finish();
	}

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