אפליקציות של תוספים ופקודות מקומיות

ערכת ה-SDK של Android Management API‏ (AMAPI) מאפשרת לאפליקציית תוסף שצוינה על ידי ה-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) כדי לקבל עדכונים לגבי שינויים בסטטוס של פקודות ממושכות, באופן הבא:

  1. שינויים בסטטוס של הפקודה מועברים אל CommandListener. צריך להטמיע את הממשק הזה באפליקציה ולציין איך מטפלים בעדכוני הסטטוס שמתקבלים.
  2. מרחיבים את NotificationReceiverService ומספקים מופע של CommandListener באמצעות השיטה getCommandListener.
  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
     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, נצטרך את הפרטים הבאים:

  1. שם החבילה של אפליקציית התוסף.
  2. גיבוב SHA-256 עם קידוד הקסדצימלי של החתימה שמשויכת לחבילת האפליקציה.
  3. אם רוצים לבדוק קריאה חוזרת (callback), אפשר לציין את השם המלא של השירות מהשירות החדש שהושק כדי לתמוך בקריאה חוזרת. (השם המלא של CommandService בדוגמה).