แอปส่วนขยายและคำสั่งในเครื่อง

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

ตัวอย่างก่อนหน้านี้แสดงให้เห็นการออกคำขอข้อมูลแอปที่ชัดเจนสำหรับแพ็กเกจที่ระบุ และการรอจนกว่าจะออกคำสั่งสำเร็จ หากส่งออกได้สำเร็จ ระบบจะแสดงผลออบเจ็กต์ Command พร้อมสถานะคำสั่งปัจจุบันและรหัสคำสั่ง ซึ่งสามารถใช้เพื่อค้นหาสถานะของคำสั่งใดๆ ที่ใช้เวลานานในภายหลังได้

รับคำสั่ง

แอปส่วนขยายสามารถค้นหาสถานะของคำขอคำสั่งที่ออกก่อนหน้านี้ได้ หากต้องการเรียกสถานะของคำสั่ง คุณจะต้องใช้รหัสคำสั่ง (มีอยู่ในคำขอคำสั่งปัญหา) ข้อมูลโค้ดต่อไปนี้แสดงวิธีส่ง 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());
  }
  ...

ฟัง Callback สำหรับการเปลี่ยนสถานะคำสั่ง

แอปส่วนขยายสามารถลงทะเบียน Callback เพื่อรับการอัปเดตสำหรับการเปลี่ยนแปลงสถานะของคำสั่งที่ใช้เวลานานโดยทำตามขั้นตอนต่อไปนี้

  1. ระบบจะแจ้งเตือนการเปลี่ยนแปลงสถานะคำสั่งไปที่ CommandListener นำอินเทอร์เฟซนี้ไปใช้ในแอป และระบุการใช้งานวิธีจัดการการอัปเดตสถานะที่ได้รับ
  2. ขยาย NotificationReceiverService และระบุอินสแตนซ์ CommandListener
  3. ระบุชื่อคลาสของ NotificationReceiverService แบบขยายในนโยบาย Android Management API (โปรดดูการกำหนดค่านโยบาย)

    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" />
    

การกำหนดค่านโยบาย

EMM จะต้องระบุนโยบาย extensionConfig เพื่อให้แอปส่วนขยายสื่อสารกับ ADP ได้โดยตรง

 "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"
   }
 }]

การทดสอบ

การทดสอบ 1 หน่วย

LocalCommandClient เป็นอินเทอร์เฟซที่ช่วยให้สามารถติดตั้งใช้งานทดสอบได้

การทดสอบการผสานรวม

ต้องใช้ข้อมูลต่อไปนี้เพื่อทดสอบกับ ADP

  1. ชื่อแพ็กเกจของแอปส่วนขยาย
  2. แฮช SHA-256 ที่เข้ารหัสเลขฐาน 16 ของลายเซ็นที่เชื่อมโยงกับแพ็กเกจแอป
  3. หรือหากต้องการทดสอบ Callback - ชื่อที่สมบูรณ์ของบริการจากบริการที่เพิ่งได้รับการแนะนำเพื่อรองรับ Callback (ชื่อที่สมบูรณ์ในตัวเองของ CommandService ในตัวอย่าง)