AppLauncher.java
Go to the documentation of this file.
00001 /*
00002 * Software License Agreement (BSD License)
00003 *
00004 * Copyright (c) 2011, Willow Garage, Inc.
00005 * Copyright (c) 2013, OSRF.
00006 * All rights reserved.
00007 * Redistribution and use in source and binary forms, with or without
00008 * modification, are permitted provided that the following conditions
00009 * are met:
00010 *
00011 * * Redistributions of source code must retain the above copyright
00012 * notice, this list of conditions and the following disclaimer.
00013 * * Redistributions in binary form must reproduce the above
00014 * copyright notice, this list of conditions and the following
00015 * disclaimer in the documentation and/or other materials provided
00016 * with the distribution.
00017 * * Neither the name of Willow Garage, Inc. nor the names of its
00018 * contributors may be used to endorse or promote products derived
00019 * from this software without specific prior written permission.
00020 *
00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 * POSSIBILITY OF SUCH DAMAGE.
00033 */
00034 
00035 package com.github.rosjava.android_remocons.robot_remocon;
00036 
00037 import java.net.URI;
00038 import java.util.ArrayList;
00039 
00040 import android.app.AlertDialog;
00041 import android.content.ActivityNotFoundException;
00042 import android.content.DialogInterface;
00043 import android.content.Intent;
00044 import android.net.Uri;
00045 import android.util.Log;
00046 
00047 import com.github.rosjava.android_apps.application_management.AppManager;
00048 import com.github.rosjava.android_apps.application_management.RobotDescription;
00049 
00050 import rocon_app_manager_msgs.PairingClient;
00051 
00052 public class AppLauncher {
00053   static private final String CLIENT_TYPE = "android";
00054 
00055 
00057   static public boolean launch(final RobotActivity parentActivity, rocon_app_manager_msgs.App app, URI uri,RobotDescription currentRobot,boolean runningNodes) {
00058     ArrayList<ClientAppData> android_apps = new ArrayList<ClientAppData>();
00059 
00060     
00061     if (parentActivity instanceof RobotRemocon) {
00062       ((RobotRemocon)parentActivity).onAppClicked(app, app.getPairingClients().size() > 0);
00063     } else {
00064       Log.i("RobotRemocon", "Could not launch because parent is not an appchooser");
00065       if (app.getPairingClients().size() == 0) {
00066         Log.e("RobotRemocon", "Not launching application!!!");
00067         return false;
00068       }
00069     }
00070 
00071     if (app.getPairingClients().size() == 0) {
00072       return false;
00073     }
00074     
00075     Log.i("RobotRemocon", "launching robot app " + app.getName() + " -> found " + app.getPairingClients().size()
00076         + " client apps.");
00077 
00078     // Loop over all possible client apps to find the android ones.
00079     for (int i = 0; i < app.getPairingClients().size(); i++) {
00080       PairingClient client_app = app.getPairingClients().get(i);
00081       if (client_app.getClientType() != null && client_app.getClientType().equals(CLIENT_TYPE)) {
00082      ClientAppData data = new ClientAppData(client_app);
00083      android_apps.add(data);
00084       }
00085     }
00086 
00087     Log.i("RobotRemocon", "launching robot app " + app.getName() + " -> found " + android_apps.size()
00088         + " compatible android apps.");
00089 
00090     // TODO: filter out android apps which are not appropriate for
00091     // this device by looking at specific entries in the manager_data_
00092     // map of each app in android_apps.
00093     ArrayList<ClientAppData> appropriateAndroidApps = android_apps;
00094 
00095 
00096     // TODO: support multiple android apps
00097     if (appropriateAndroidApps.size() != 1) {
00098       AlertDialog.Builder dialog = new AlertDialog.Builder(parentActivity);
00099       dialog.setTitle("Wrong Number of Android Apps");
00100       dialog.setMessage("There are " + appropriateAndroidApps.size() + " valid android apps, not 1 as there should be.");
00101       dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
00102           @Override
00103           public void onClick(DialogInterface dlog, int i) {
00104             dlog.dismiss();
00105           }});
00106       dialog.show();
00107       return false;
00108     }
00109 
00110     //TODO: this installs the last android app in the set.
00111     String className = "";
00112 
00113     // Loop over all android apps, trying to launch one.
00114     for (int i = 0; i < appropriateAndroidApps.size(); i++) {
00115       ClientAppData appData = appropriateAndroidApps.get(i);
00116       Intent intent = appData.createIntent();
00117       intent.putExtra(AppManager.PACKAGE + ".robot_app_name", app.getName());
00118       intent.putExtra(RobotDescription.UNIQUE_KEY, currentRobot);
00119       intent.putExtra("ChooserURI", uri.toString());
00120       intent.putExtra("runningNodes", runningNodes);
00121       intent.putExtra("PairedManagerActivity", "com.github.robotics_in_concert.rocon_android.robot_remocon.RobotRemocon");
00122       try {
00123         className = intent.getAction();
00124         Log.i("RobotRemocon", "trying to startActivity( action: " + intent.getAction() + " )");
00125         parentActivity.startActivity(intent);
00126         return true;
00127       } catch (ActivityNotFoundException e) {
00128         Log.i("RobotRemocon", "activity not found for action: " + intent.getAction());
00129       }
00130     }
00131 
00132     final String installPackage = className.substring(0, className.lastIndexOf("."));
00133 
00134     Log.i("RobotRemocon", "showing not-installed dialog.");
00135 
00136     // TODO:
00137     // Loop over all android apps, trying to install one. (??)
00138     // For now, just show a failure dialog.
00139     AlertDialog.Builder dialog = new AlertDialog.Builder(parentActivity);
00140     dialog.setTitle("Android app not installed.");
00141     dialog
00142         .setMessage("This robot app requires a client user interface app, but none of the applicable android apps are installed. Would you like to install the app from the market place?");
00143     dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
00144       @Override
00145       public void onClick(DialogInterface dlog, int i) {
00146         Uri uri = Uri.parse("market://details?id=" + installPackage);
00147         Intent intent = new Intent(Intent.ACTION_VIEW, uri);
00148         parentActivity.startActivity(intent);
00149       }
00150     });
00151     dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
00152       @Override
00153       public void onClick(DialogInterface dlog, int i) {
00154         dlog.dismiss();
00155       }
00156     });
00157     dialog.show();
00158     return false;
00159   }
00160 }


android_remocons
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Wed Aug 26 2015 10:40:28