與 AMAPI SDK 整合

AMAPI SDK 可讓 EMM 指定的擴充功能應用程式直接與 Android Device Policy 通訊。目前支援本機執行 Commands,且僅支援 ClearAppData 指令。您必須採取下列步驟,才能與 SDK 整合:

  1. 將程式庫新增至擴充功能應用程式
  2. 利用我們提供的 API 視需要發出指令
  3. 新增查詢元素 (如果目標 SDK 大於或等於 30)。
  4. 您也可以選擇提供服務的實作方式,用於監聽指令狀態變更回呼
  5. 使用擴充政策佈建裝置。

必要條件

  • 確保擴充功能應用程式的 minSdkVersion 至少設為 API 級別 21。

正在將程式庫新增至擴充功能應用程式

在頂層 build.gradle 檔案中,將包含 SDK 程式庫的 Google Maven 存放區新增至相關模組,並將依附元件新增至程式庫:

repositories {
  ...
  google()
}

然後將程式庫新增至模組的依附元件區塊:

dependencies {
  implementation 'com.google.android.libraries.enterprise.amapi:amapi:1.0.0'
}

傳送要求至 Android Device Policy

您現在應該可以向 ADP 傳送要求。系統支援下列要求。

問題指令

擴充功能應用程式可要求使用 ADP 發出的指令。IssueCommandRequest 包含要求物件,當中詳述要核發的指令和特定參數。詳情請參閱 Javadoc。

下列程式碼片段說明如何發出清除套件資料的要求:

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,方便您查詢任何長時間執行指令的狀態。

取得指令

擴充功能應用程式也可以查詢先前發出的指令要求狀態。如要擷取指令的狀態,您必須具備指令 ID (可從問題指令要求中找到)。下列程式碼片段示範如何傳送 GetCommand 要求至 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());
  }
  ...

新增查詢元素

如果應用程式指定的是 SDK 30 以上版本,則資訊清單中必須查詢元素,才能指定與 ADP 互動。

<queries>
    <package android:name="com.google.android.apps.work.clouddpc" />
</queries>

詳情請參閱「在 Android 上篩選套件瀏覽權限 」。

監聽指令狀態變更回呼

  1. 指令狀態變更會收到 CommandListener 的通知,並在應用程式中實作這個介面,並提供如何處理接收狀態更新。
  2. 擴充 NotificationReceiverService,並提供 CommandListener 執行個體。
  3. 指定 Android Management API 政策中的 Extended 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" />
    

政策設定

 "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 是介面,因此進行測試可讓您輕鬆提供可測試的實作內容。

整合測試

需要以下資訊才能測試 Android Device Policy:

  1. 擴充功能應用程式的套件名稱。
  2. 與應用程式套件相關聯簽名的十六進位編碼 SHA-256 雜湊。
  3. 或者在測試回呼時選用:新引進服務支援回呼的完整名稱。(範例中為 CommandService 的完整名稱)。