拡張機能アプリとローカル コマンド

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(issue コマンド リクエストから取得可能)が必要です。次のスニペットは、GetCommandRequest を ADP に送信する方法を示しています。

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());
  }
  ...

コマンド ステータス変更のコールバックをリッスンする

拡張機能アプリは、次の手順に沿って、長時間実行コマンドのステータス変更の更新を受信するためのコールバックを登録できます。

  1. コマンド ステータスの変更は CommandListener に通知され、このインターフェースをアプリに実装して、受信したステータス更新の処理方法を実装します。
  2. NotificationReceiverService を拡張し、CommandListener インスタンスを指定します。
  3. Android Management API ポリシーで、拡張された NotificationReceiverService のクラス名を指定します(ポリシー構成を参照)。

    import com.google.android.managementapi.commands.CommandListener;
    import com.google.android.managementapi.notification.NotificationReceiverService;
    
    ...
    
    public class SampleCommandService extends NotificationReceiverService {
    
      @Override
      protected void setupInjection() {
        // (Optional) If using DI and needs initialisation then use this method.
      }
    
      @Override
      public CommandListener getCommandListener() {
        // return the concrete implementation from previous step
        return ...;
      }
    }
    
  4. サービスを AndroidManifest.xml に追加し、エクスポートされたことを確認します。

    <service
     android:name = ".notification.SampleCommandService"
     android:exported = "true" />
    

ポリシーの構成

拡張機能アプリが 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 を使用してテストするには、次の情報が必要です。

  1. 拡張機能アプリのパッケージ名。
  2. アプリ パッケージに関連付けられた署名の 16 進数でエンコードされた SHA-256 ハッシュ。
  3. (省略可)コールバックをテストする場合 - コールバックをサポートする、新しく導入されたサービスのサービスの完全修飾名。(この例では CommandService の完全修飾名)。