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

Android Management API(AMAPI)SDK を使用すると、EMM 指定の拡張アプリが Android Device Policy(ADP)と直接通信し、デバイスで Commands を実行できます。

AMAPI SDK との統合では、このライブラリとアプリケーションへの追加方法について詳しく説明しています。

SDK を統合すると、拡張機能アプリは ADP と通信して次のことができます。

Issue Command(コマンドの発行)

拡張機能アプリは、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 は、後で長時間実行コマンドのステータスをクエリするために使用できます。

Get コマンド

拡張機能アプリは、以前に発行されたコマンド リクエストのステータスをクエリできます。コマンドのステータスを取得するには、コマンド 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());
  }
  ...

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

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

  1. コマンド ステータスの変更は CommandListener に通知されます。アプリでこのインターフェースを実装し、受信したステータス更新を処理する方法の実装を提供します。
  2. NotificationReceiverService を拡張し、getCommandListener メソッドを介して CommandListener インスタンスを提供します。
  3. アプリケーション ポリシーを構成して、アプリに COMPANION_APP ロールを割り当てます(ポリシーの構成をご覧ください)。

    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 がアプリケーション ポリシーの roles フィールドを使用して、アプリに COMPANION_APP ロールを割り当てる必要があります。

 "applications": [{
   "packageName": "com.amapi.extensibility.demo",
   "installType": "FORCE_INSTALLED",
   "roles": [
     { "roleType": "COMPANION_APP" }
   ]
 }]

利用可能なその他のオプションについては、アプリロール ポリシーを使用してデバイスをプロビジョニングするをご覧ください。

テスト

単体テスト

LocalCommandClient はインターフェースであるため、テスト可能な実装を提供できます。

統合テスト

ADP でテストするには、次の情報が必要です。

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