Home | History | Annotate | Download | only in activities
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      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
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     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.
     15  */
     16 
     17 package com.android.tools.sdkcontroller.activities;
     18 
     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;
     30 
     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;
     35 
     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.
     40  */
     41 public class MainActivity extends BaseBindingActivity {
     42 
     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;
     51 
     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);
     57 
     58         mTextError  = (TextView) findViewById(R.id.textError);
     59         mTextStatus = (TextView) findViewById(R.id.textStatus);
     60 
     61         WebView wv = (WebView) findViewById(R.id.webIntro);
     62         wv.loadUrl("file:///android_asset/intro_help.html");
     63 
     64         setupButtons();
     65     }
     66 
     67     @Override
     68     protected void onResume() {
     69         // BaseBindingActivity.onResume will bind to the service.
     70         super.onResume();
     71         updateError();
     72     }
     73 
     74     @Override
     75     protected void onPause() {
     76         // BaseBindingActivity.onResume will unbind from (but not stop) the service.
     77         super.onPause();
     78     }
     79 
     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();
     87     }
     88 
     89     // ----------
     90 
     91     @Override
     92     protected void onServiceConnected() {
     93         updateButtons();
     94     }
     95 
     96     @Override
     97     protected void onServiceDisconnected() {
     98         updateButtons();
     99     }
    100 
    101     @Override
    102     protected ControllerListener createControllerListener() {
    103         return new MainControllerListener();
    104     }
    105 
    106     // ----------
    107 
    108     private void setupButtons() {
    109         mBtnOpenMultitouch = (Button) findViewById(R.id.btnOpenMultitouch);
    110         mBtnOpenSensors    = (Button) findViewById(R.id.btnOpenSensors);
    111 
    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);
    118             }
    119         });
    120 
    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);
    127             }
    128         });
    129 
    130         mBtnToggleService = (ToggleButton) findViewById(R.id.toggleService);
    131 
    132         // set initial state
    133         updateButtons();
    134 
    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();
    144                 }
    145             }
    146         });
    147 
    148     }
    149 
    150     private void updateButtons() {
    151         boolean running = ControllerService.isServiceIsRunning();
    152         mBtnOpenMultitouch.setEnabled(running);
    153         mBtnOpenSensors.setEnabled(running);
    154         mBtnToggleService.setChecked(running);
    155     }
    156 
    157     /**
    158      * Unbind and then actually stops the service.
    159      */
    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);
    165     }
    166 
    167     private class MainControllerListener implements ControllerListener {
    168         @Override
    169         public void onErrorChanged() {
    170             runOnUiThread(new Runnable() {
    171                 @Override
    172                 public void run() {
    173                     updateError();
    174                 }
    175             });
    176         }
    177 
    178         @Override
    179         public void onStatusChanged() {
    180             runOnUiThread(new Runnable() {
    181                 @Override
    182                 public void run() {
    183                     updateStatus();
    184                 }
    185             });
    186         }
    187     }
    188 
    189     private void updateError() {
    190         ControllerBinder binder = getServiceBinder();
    191         String error = binder == null ? "" : binder.getServiceError();
    192         if (error == null) {
    193             error = "";
    194         }
    195 
    196         mTextError.setVisibility(error.length() == 0 ? View.GONE : View.VISIBLE);
    197         mTextError.setText(error);
    198     }
    199 
    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));
    206 
    207     }
    208 }