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 17 package com.android.dialer.commandline; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.text.TextUtils; 23 import com.android.dialer.buildtype.BuildType; 24 import com.android.dialer.buildtype.BuildType.Type; 25 import com.android.dialer.commandline.Command.IllegalCommandLineArgumentException; 26 import com.android.dialer.common.LogUtil; 27 import com.google.common.util.concurrent.FutureCallback; 28 import com.google.common.util.concurrent.Futures; 29 import com.google.common.util.concurrent.MoreExecutors; 30 31 /** 32 * Receives broadcasts to the component from adb shell. Must be on bugfood or have debug logging 33 * enabled. 34 */ 35 public class CommandLineReceiver extends BroadcastReceiver { 36 37 public static final String COMMAND = "command"; 38 public static final String ARGS = "args"; 39 public static final String TAG = "tag"; 40 41 @Override onReceive(Context context, Intent intent)42 public void onReceive(Context context, Intent intent) { 43 String outputTag = intent.getStringExtra(TAG); 44 if (outputTag == null) { 45 LogUtil.e("CommandLineReceiver", "missing tag"); 46 return; 47 } 48 if (!LogUtil.isDebugEnabled() && BuildType.get() != Type.BUGFOOD) { 49 LogUtil.i(outputTag, "DISABLED"); 50 return; 51 } 52 Command command = 53 CommandLineComponent.get(context) 54 .commandSupplier() 55 .get() 56 .get(intent.getStringExtra(COMMAND)); 57 try { 58 if (command == null) { 59 LogUtil.i(outputTag, "unknown command " + intent.getStringExtra(COMMAND)); 60 return; 61 } 62 63 Arguments args = Arguments.parse(intent.getStringArrayExtra(ARGS)); 64 65 if (args.getBoolean("help", false)) { 66 LogUtil.i(outputTag, "usage:\n" + command.getUsage()); 67 return; 68 } 69 Futures.addCallback( 70 command.run(args), 71 new FutureCallback<String>() { 72 @Override 73 public void onSuccess(String response) { 74 if (TextUtils.isEmpty(response)) { 75 LogUtil.i(outputTag, "EMPTY"); 76 } else { 77 LogUtil.i(outputTag, response); 78 } 79 } 80 81 @Override 82 public void onFailure(Throwable throwable) { 83 if (throwable instanceof IllegalCommandLineArgumentException) { 84 LogUtil.e(outputTag, throwable.getMessage() + "\n\nusage:\n" + command.getUsage()); 85 } 86 LogUtil.e(outputTag, "error running command future", throwable); 87 } 88 }, 89 MoreExecutors.directExecutor()); 90 } catch (IllegalCommandLineArgumentException e) { 91 LogUtil.e(outputTag, e.getMessage() + "\n\nusage:\n" + command.getUsage()); 92 } catch (Throwable throwable) { 93 LogUtil.e(outputTag, "error running command", throwable); 94 } 95 } 96 } 97