Android Management API(AMAPI)SDK を使用すると、EMM で指定された拡張アプリが Android Device Policy(ADP)と直接通信し、デバイスで Commands を実行できます。
このライブラリとアプリへの追加方法について詳しくは、AMAPI SDK と統合するをご覧ください。
SDK を統合すると、拡張機能アプリは ADP と通信して次のことができます。
コマンドの発行
拡張機能アプリは、ADP を使用してコマンドの発行をリクエストできます。IssueCommandRequest には、発行されるコマンドと特定のパラメータの詳細を含むリクエスト オブジェクトが含まれます。
次のスニペットは、パッケージのデータを消去するリクエストを発行する方法を示しています。
import android.util.Log;
...
import com.google.android.managementapi.commands.LocalCommandClientFactory;
import com.google.android.managementapi.commands.model.Command;
import com.google.android.managementapi.commands.model.GetCommandRequest;
import com.google.android.managementapi.commands.model.IssueCommandRequest;
import com.google.android.managementapi.commands.model.IssueCommandRequest.ClearAppsData;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
...
  void issueClearAppDataCommand(ImmutableList<String> packageNames) {
    Futures.addCallback(
        LocalCommandClientFactory.create(getContext())
            .issueCommand(createClearAppRequest(packageNames)),
        new FutureCallback<Command>() {
          @Override
          public void onSuccess(Command result) {
            // Process the returned command result here
            Log.i(TAG, "Successfully issued command");
          }
          @Override
          public void onFailure(Throwable t) {
            Log.e(TAG, "Failed to issue command", t);
          }
        },
        MoreExecutors.directExecutor());
  }
  IssueCommandRequest createClearAppRequest(ImmutableList<String> packageNames) {
    return IssueCommandRequest.builder()
        .setClearAppsData(
            ClearAppsData.builder()
                .setPackageNames(packageNames)
                .build()
        )
        .build();
  }
...
上の例は、指定したパッケージのアプリデータの消去リクエストを発行し、コマンドが正常に発行されるまで待機しています。正常に実行されると、現在のコマンド ステータスとコマンド ID を含む Command オブジェクトが返されます。この ID は、後で長時間実行コマンドのステータスをクエリするために使用できます。
取得コマンド
拡張機能アプリは、以前に発行されたコマンド リクエストのステータスをクエリできます。コマンドのステータスを取得するには、コマンド ID(問題のコマンド リクエストで取得可能)が必要です。次のスニペットは、ADP に GetCommandRequest を送信する方法を示しています。
import android.util.Log;
...
import com.google.android.managementapi.commands.LocalCommandClientFactory;
...
import com.google.android.managementapi.commands.model.GetCommandRequest;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.MoreExecutors;
...
  void getCommand(String commandId) {
    Futures.addCallback(
        LocalCommandClientFactory.create(getApplication())
            .getCommand(GetCommandRequest.builder().setCommandId(commandId).build()),
        new FutureCallback<Command>() {
          @Override
          public void onSuccess(Command result) {
            // Process the returned command result here
            Log.i(Constants.TAG, "Successfully issued command");
          }
          @Override
          public void onFailure(Throwable t) {
            Log.e(Constants.TAG, "Failed to issue command", t);
          }
        },
        MoreExecutors.directExecutor());
  }
  ...
コマンドのステータス変更コールバックをリッスンする
拡張機能アプリは、必要に応じてコールバックを登録して、長時間実行コマンドのステータス変更の更新を受け取ることができます。手順は次のとおりです。
- コマンドのステータスの変更は CommandListenerに通知されます。このインターフェースをアプリに実装し、受信したステータス更新を処理する方法の実装を提供します。
- NotificationReceiverServiceを拡張し、- getCommandListenerメソッドで- CommandListenerインスタンスを指定します。
- Android Management API ポリシーで拡張 - NotificationReceiverServiceのクラス名を指定します(ポリシー構成をご覧ください)。- import com.google.android.managementapi.commands.CommandListener; import com.google.android.managementapi.notification.NotificationReceiverService; ... public class SampleCommandService extends NotificationReceiverService { @Override public CommandListener getCommandListener() { // return the concrete implementation from previous step return ...; } }
ポリシーの構成
拡張機能アプリが ADP と直接通信できるようにするには、EMM が extensionConfig ポリシーを提供する必要があります。
 "applications": [{
   "packageName": "com.amapi.extensibility.demo",
   ...
   "extensionConfig": {
     "signingKeyFingerprintsSha256": [
       // Include signing key of extension app
     ],
     // Optional if callback is implemented
     "notificationReceiver": "com.amapi.extensibility.demo.notification.SampleCommandService"
   }
 }]
テスト
単体テスト
LocalCommandClient はインターフェースであるため、テスト可能な実装を提供できます。
統合テスト
ADP でテストするには、次の情報が必要になります。
- 拡張機能アプリのパッケージ名。
- アプリ パッケージに関連付けられた署名の 16 進数でエンコードされた SHA-256 ハッシュ。
- 必要に応じて、コールバックをテストする場合 - コールバックをサポートするために新しく導入されたサービスの完全修飾名。(例では CommandServiceの完全修飾名)。
