Roll NDK to pick std::deque patch.
[android_tools.git] / sdk / tools / apps / SdkController / src / com / android / tools / sdkcontroller / activities / MainActivity.java
blob47692454cc07d0d322df73fd054cc9d35bd430d2
1 /*
2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
17 package com.android.tools.sdkcontroller.activities;
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.webkit.WebView;
25 import android.widget.Button;
26 import android.widget.CompoundButton;
27 import android.widget.CompoundButton.OnCheckedChangeListener;
28 import android.widget.TextView;
29 import android.widget.ToggleButton;
31 import com.android.tools.sdkcontroller.R;
32 import com.android.tools.sdkcontroller.service.ControllerService;
33 import com.android.tools.sdkcontroller.service.ControllerService.ControllerBinder;
34 import com.android.tools.sdkcontroller.service.ControllerService.ControllerListener;
36 /**
37 * Main activity. It's the entry point for the application.
38 * It allows the user to start/stop the service and see it's current state and errors.
39 * It also has buttons to start either the sensor control activity or the multitouch activity.
41 public class MainActivity extends BaseBindingActivity {
43 @SuppressWarnings("hiding")
44 public static String TAG = MainActivity.class.getSimpleName();
45 private static boolean DEBUG = true;
46 private Button mBtnOpenMultitouch;
47 private Button mBtnOpenSensors;
48 private ToggleButton mBtnToggleService;
49 private TextView mTextError;
50 private TextView mTextStatus;
52 /** Called when the activity is first created. */
53 @Override
54 public void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
56 setContentView(R.layout.main);
58 mTextError = (TextView) findViewById(R.id.textError);
59 mTextStatus = (TextView) findViewById(R.id.textStatus);
61 WebView wv = (WebView) findViewById(R.id.webIntro);
62 wv.loadUrl("file:///android_asset/intro_help.html");
64 setupButtons();
67 @Override
68 protected void onResume() {
69 // BaseBindingActivity.onResume will bind to the service.
70 super.onResume();
71 updateError();
74 @Override
75 protected void onPause() {
76 // BaseBindingActivity.onResume will unbind from (but not stop) the service.
77 super.onPause();
80 @Override
81 public void onBackPressed() {
82 if (DEBUG) Log.d(TAG, "onBackPressed");
83 // If back is pressed, we stop the service automatically.
84 // It seems more intuitive that way.
85 stopService();
86 super.onBackPressed();
89 // ----------
91 @Override
92 protected void onServiceConnected() {
93 updateButtons();
96 @Override
97 protected void onServiceDisconnected() {
98 updateButtons();
101 @Override
102 protected ControllerListener createControllerListener() {
103 return new MainControllerListener();
106 // ----------
108 private void setupButtons() {
109 mBtnOpenMultitouch = (Button) findViewById(R.id.btnOpenMultitouch);
110 mBtnOpenSensors = (Button) findViewById(R.id.btnOpenSensors);
112 mBtnOpenMultitouch.setOnClickListener(new OnClickListener() {
113 @Override
114 public void onClick(View v) {
115 // Open the multi-touch activity.
116 Intent i = new Intent(MainActivity.this, MultiTouchActivity.class);
117 startActivity(i);
121 mBtnOpenSensors.setOnClickListener(new OnClickListener() {
122 @Override
123 public void onClick(View v) {
124 // Open the sensor activity.
125 Intent i = new Intent(MainActivity.this, SensorActivity.class);
126 startActivity(i);
130 mBtnToggleService = (ToggleButton) findViewById(R.id.toggleService);
132 // set initial state
133 updateButtons();
135 mBtnToggleService.setOnCheckedChangeListener(new OnCheckedChangeListener() {
136 @Override
137 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
138 if (isChecked) {
139 bindToService();
140 updateButtons();
141 } else {
142 stopService();
143 updateButtons();
150 private void updateButtons() {
151 boolean running = ControllerService.isServiceIsRunning();
152 mBtnOpenMultitouch.setEnabled(running);
153 mBtnOpenSensors.setEnabled(running);
154 mBtnToggleService.setChecked(running);
158 * Unbind and then actually stops the service.
160 private void stopService() {
161 Intent service = new Intent(this, ControllerService.class);
162 unbindFromService();
163 if (DEBUG) Log.d(TAG, "stop service requested");
164 stopService(service);
167 private class MainControllerListener implements ControllerListener {
168 @Override
169 public void onErrorChanged() {
170 runOnUiThread(new Runnable() {
171 @Override
172 public void run() {
173 updateError();
178 @Override
179 public void onStatusChanged() {
180 runOnUiThread(new Runnable() {
181 @Override
182 public void run() {
183 updateStatus();
189 private void updateError() {
190 ControllerBinder binder = getServiceBinder();
191 String error = binder == null ? "" : binder.getServiceError();
192 if (error == null) {
193 error = "";
196 mTextError.setVisibility(error.length() == 0 ? View.GONE : View.VISIBLE);
197 mTextError.setText(error);
200 private void updateStatus() {
201 ControllerBinder binder = getServiceBinder();
202 boolean connected = binder == null ? false : binder.isEmuConnected();
203 mTextStatus.setText(
204 getText(connected ? R.string.main_service_status_connected
205 : R.string.main_service_status_disconnected));