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
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
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
;
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. */
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");
68 protected void onResume() {
69 // BaseBindingActivity.onResume will bind to the service.
75 protected void onPause() {
76 // BaseBindingActivity.onResume will unbind from (but not stop) the service.
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.
86 super.onBackPressed();
92 protected void onServiceConnected() {
97 protected void onServiceDisconnected() {
102 protected ControllerListener
createControllerListener() {
103 return new MainControllerListener();
108 private void setupButtons() {
109 mBtnOpenMultitouch
= (Button
) findViewById(R
.id
.btnOpenMultitouch
);
110 mBtnOpenSensors
= (Button
) findViewById(R
.id
.btnOpenSensors
);
112 mBtnOpenMultitouch
.setOnClickListener(new OnClickListener() {
114 public void onClick(View v
) {
115 // Open the multi-touch activity.
116 Intent i
= new Intent(MainActivity
.this, MultiTouchActivity
.class);
121 mBtnOpenSensors
.setOnClickListener(new OnClickListener() {
123 public void onClick(View v
) {
124 // Open the sensor activity.
125 Intent i
= new Intent(MainActivity
.this, SensorActivity
.class);
130 mBtnToggleService
= (ToggleButton
) findViewById(R
.id
.toggleService
);
135 mBtnToggleService
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
137 public void onCheckedChanged(CompoundButton buttonView
, boolean isChecked
) {
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);
163 if (DEBUG
) Log
.d(TAG
, "stop service requested");
164 stopService(service
);
167 private class MainControllerListener
implements ControllerListener
{
169 public void onErrorChanged() {
170 runOnUiThread(new Runnable() {
179 public void onStatusChanged() {
180 runOnUiThread(new Runnable() {
189 private void updateError() {
190 ControllerBinder binder
= getServiceBinder();
191 String error
= binder
== null ?
"" : binder
.getServiceError();
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();
204 getText(connected ? R
.string
.main_service_status_connected
205 : R
.string
.main_service_status_disconnected
));