Home | History | Annotate | Download | only in intentplayground
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.android.intentplayground;
     17 
     18 import android.content.ComponentName;
     19 import android.content.Intent;
     20 import android.util.Log;
     21 
     22 import java.io.BufferedReader;
     23 import java.io.IOException;
     24 import java.io.InputStreamReader;
     25 
     26 /**
     27  * Provides a backend for launching activities using the ActivityManager command line.
     28  */
     29 class AMControl {
     30     public static final String TAG = "AMControl";
     31 
     32     /**
     33      * Launches the activity specified by an {@link Intent} in the background.
     34      * @param intent The intent to launch.
     35      * @return The output of the "am shell" command.
     36      */
     37     public static String launchInBackground(Intent intent) {
     38         StringBuilder cmd = new StringBuilder("am start -n ");
     39         ComponentName target = intent.getComponent();
     40         cmd.append(target.getPackageName()).append("/").append(target.getShortClassName());
     41         cmd.append(" -f ").append("0x").append(Integer.toHexString(intent.getFlags()));
     42         cmd.append(" --ez moveToBack true");
     43         return execCmd(cmd.toString());
     44     }
     45 
     46     /**
     47      * Executes a shell command in a separate process.
     48      * @param cmd The command to execute.
     49      * @return The output of the command.
     50      */
     51     public static String execCmd(String cmd) {
     52         StringBuilder output = new StringBuilder();
     53         ProcessBuilder factory = new ProcessBuilder(cmd.split(" "));
     54         String line;
     55         int lineCount = 0;
     56         if (BuildConfig.DEBUG) Log.d(TAG, "Running command " + cmd);
     57         try {
     58             Process proc = factory.start();
     59             // get stdout
     60             BufferedReader reader = new BufferedReader(new InputStreamReader(
     61                     proc.getInputStream()));
     62             while ((line = reader.readLine()) != null) {
     63                 output.append(line).append('\n');
     64                 lineCount++;
     65             }
     66             reader.close();
     67             // get stderr
     68             reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
     69             while ((line = reader.readLine()) != null) {
     70                 output.append(line).append('\n');
     71                 lineCount++;
     72             }
     73             reader.close();
     74             if (BuildConfig.DEBUG) {
     75                 Log.d(TAG, String.format("Received %d lines from %s:\n %s",
     76                         lineCount, cmd.split(" ")[0], output.toString()));
     77             }
     78         } catch (IOException e) {
     79             if (BuildConfig.DEBUG) Log.e(TAG, output.append(e.getMessage()).toString());
     80             throw new RuntimeException(e);
     81         }
     82         return output.toString();
     83     }
     84 }
     85