ModuleInstallClient এর সাথে API প্রাপ্যতা নিশ্চিত করা

Google Play পরিষেবাগুলির সংক্ষিপ্ত বিবরণ নিবন্ধে বর্ণিত হিসাবে, Google-প্রত্যয়িত Android ডিভাইসগুলিতে Google Play পরিষেবাগুলি দ্বারা চালিত SDKগুলি অন-ডিভাইস পরিষেবাগুলির দ্বারা সমর্থিত। ডিভাইসের পুরো ফ্লিট জুড়ে স্টোরেজ এবং মেমরি সংরক্ষণ করতে, কিছু পরিষেবা চাহিদা অনুযায়ী ইনস্টল করা হয় যখন এটি স্পষ্ট হয় যে একটি নির্দিষ্ট ডিভাইসের প্রাসঙ্গিক কার্যকারিতা প্রয়োজন। উদাহরণস্বরূপ, Google Play পরিষেবাগুলিতে মডেলগুলি ব্যবহার করার সময় ML Kit এই বিকল্পটি প্রদান করে

SDK-এর AndroidManifest.xml এ নির্ভরতার উপর ভিত্তি করে একটি পরিষেবা (বা "মডিউল") ডাউনলোড এবং ইনস্টল করার জন্য একটি অ্যাপের সমান্তরালে এটির প্রয়োজনের জন্য সবচেয়ে সাধারণ ঘটনা। আরও নিয়ন্ত্রণের জন্য, মডিউল ইনস্টল এপিআইগুলি স্পষ্টভাবে মডিউল প্রাপ্যতা পরীক্ষা করার, মডিউল ইনস্টলেশনের অনুরোধ, অনুরোধের অবস্থা নিরীক্ষণ এবং ত্রুটিগুলি পরিচালনা করার ক্ষমতা প্রদান করে।

ModuleInstallClient এর সাথে API উপলব্ধতা নিশ্চিত করতে নীচের পদক্ষেপগুলি অনুসরণ করুন৷ মনে রাখবেন যে নীচের কোড স্নিপেটগুলি TensorFlow Lite SDK ( play-services-tflite-java ) একটি উদাহরণ লাইব্রেরি হিসাবে ব্যবহার করে, কিন্তু এই পদক্ষেপগুলি OptionalModuleApi এর সাথে একীভূত যে কোনও লাইব্রেরির জন্য প্রযোজ্য। এই নির্দেশিকাটি অতিরিক্ত তথ্য সহ আপডেট করা হবে কারণ আরও SDK সমর্থন যোগ করবে৷

তুমি শুরু করার আগে

আপনার অ্যাপ প্রস্তুত করতে, নিম্নলিখিত বিভাগগুলিতে পদক্ষেপগুলি সম্পূর্ণ করুন৷

অ্যাপের পূর্বশর্ত

নিশ্চিত করুন যে আপনার অ্যাপের বিল্ড ফাইল নিম্নলিখিত মানগুলি ব্যবহার করে:

  • 19 বা তার বেশির একটি minSdkVersion

আপনার অ্যাপ কনফিগার করুন

  1. আপনার টপ-লেভেল settings.gradle ফাইলে, Google-এর Maven রিপোজিটরি এবং Maven সেন্ট্রাল রিপোজিটরি dependencyResolutionManagement ব্লকের মধ্যে অন্তর্ভুক্ত করুন:

    dependencyResolutionManagement {
        repositories {
            google()
            mavenCentral()
        }
    }
    
  2. আপনার মডিউলের Gradle বিল্ড ফাইলে (সাধারণত app/build.gradle ), play-services-base এবং play-services-tflite-java এর জন্য Google Play পরিষেবা নির্ভরতা যোগ করুন:

    dependencies {
      implementation 'com.google.android.gms:play-services-base:18.4.0'
      implementation 'com.google.android.gms:play-services-tflite-java:16.2.0-beta02'
    }
    

মডিউল প্রাপ্যতা পরীক্ষা করুন

  1. ModuleInstallClient এর একটি উদাহরণ পান:

    কোটলিন

    val moduleInstallClient = ModuleInstall.getClient(context)
    

    জাভা

    ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
    
  2. OptionalModuleApi ব্যবহার করে একটি ঐচ্ছিক মডিউলের প্রাপ্যতা পরীক্ষা করুন:

    কোটলিন

    val optionalModuleApi = TfLite.getClient(context)
    moduleInstallClient
      .areModulesAvailable(optionalModuleApi)
      .addOnSuccessListener {
        if (it.areModulesAvailable()) {
          // Modules are present on the device...
        } else {
          // Modules are not present on the device...
        }
      }
      .addOnFailureListener {
        // Handle failure...
      }
    

    জাভা

    OptionalModuleApi optionalModuleApi = TfLite.getClient(context);
    moduleInstallClient
        .areModulesAvailable(optionalModuleApi)
        .addOnSuccessListener(
            response -> {
              if (response.areModulesAvailable()) {
                // Modules are present on the device...
              } else {
                // Modules are not present on the device...
              }
            })
        .addOnFailureListener(
            e -> {
              // Handle failure…
            });
    

একটি বিলম্বিত ইনস্টল অনুরোধ পাঠান

  1. ModuleInstallClient এর একটি উদাহরণ পান:

    কোটলিন

    val moduleInstallClient = ModuleInstall.getClient(context)
    

    জাভা

    ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
    
  2. বিলম্বিত অনুরোধ পাঠান:

    কোটলিন

    val optionalModuleApi = TfLite.getClient(context)
    moduleInstallClient.deferredInstall(optionalModuleApi)
    

    জাভা

    OptionalModuleApi optionalModuleApi = TfLite.getClient(context);
    moduleInstallClient.deferredInstall(optionalModuleApi);
    

একটি জরুরী মডিউল ইনস্টল অনুরোধ পাঠান

  1. ModuleInstallClient এর একটি উদাহরণ পান:

    কোটলিন

    val moduleInstallClient = ModuleInstall.getClient(context)
    

    জাভা

    ModuleInstallClient moduleInstallClient = ModuleInstall.getClient(context);
    
  2. (ঐচ্ছিক) ইনস্টল স্ট্যাটাস আপডেট পরিচালনা করতে একটি InstallStatusListener তৈরি করুন।

    আপনি যদি একটি কাস্টমাইজড UI (উদাহরণস্বরূপ, একটি অগ্রগতি বার) ডাউনলোডের অগ্রগতি নিরীক্ষণ করতে চান, তাহলে আপনি ইনস্টল স্ট্যাটাস আপডেট পেতে একটি InstallStatusListener তৈরি করতে পারেন।

    কোটলিন

    inner class ModuleInstallProgressListener : InstallStatusListener {
      override fun onInstallStatusUpdated(update: ModuleInstallStatusUpdate) {
        // Progress info is only set when modules are in the progress of downloading.
        update.progressInfo?.let {
          val progress = (it.bytesDownloaded * 100 / it.totalBytesToDownload).toInt()
          // Set the progress for the progress bar.
          progressBar.setProgress(progress)
        }
    
        if (isTerminateState(update.installState)) {
          moduleInstallClient.unregisterListener(this)
        }
      }
    
      fun isTerminateState(@InstallState state: Int): Boolean {
        return state == STATE_CANCELED || state == STATE_COMPLETED || state == STATE_FAILED
      }
    }
    
    val listener = ModuleInstallProgressListener()
    

    জাভা

    static final class ModuleInstallProgressListener implements InstallStatusListener {
        @Override
        public void onInstallStatusUpdated(ModuleInstallStatusUpdate update) {
          ProgressInfo progressInfo = update.getProgressInfo();
          // Progress info is only set when modules are in the progress of downloading.
          if (progressInfo != null) {
            int progress =
                (int)
                    (progressInfo.getBytesDownloaded() * 100 / progressInfo.getTotalBytesToDownload());
            // Set the progress for the progress bar.
            progressBar.setProgress(progress);
          }
          // Handle failure status maybe…
    
          // Unregister listener when there are no more install status updates.
          if (isTerminateState(update.getInstallState())) {
    
            moduleInstallClient.unregisterListener(this);
          }
        }
    
        public boolean isTerminateState(@InstallState int state) {
          return state == STATE_CANCELED || state == STATE_COMPLETED || state == STATE_FAILED;
        }
      }
    
    InstallStatusListener listener = new ModuleInstallProgressListener();
    
  3. ModuleInstallRequest কনফিগার করুন এবং অনুরোধে OptionalModuleApi যোগ করুন:

    কোটলিন

    val optionalModuleApi = TfLite.getClient(context)
    val moduleInstallRequest =
      ModuleInstallRequest.newBuilder()
        .addApi(optionalModuleApi)
        // Add more APIs if you would like to request multiple optional modules.
        // .addApi(...)
        // Set the listener if you need to monitor the download progress.
        // .setListener(listener)
        .build()
    

    জাভা

    OptionalModuleApi optionalModuleApi = TfLite.getClient(context);
    ModuleInstallRequest moduleInstallRequest =
        ModuleInstallRequest.newBuilder()
            .addApi(optionalModuleApi)
            // Add more API if you would like to request multiple optional modules
            //.addApi(...)
            // Set the listener if you need to monitor the download progress
            //.setListener(listener)
            .build();
    
  4. ইনস্টল করার অনুরোধ পাঠান:

    কোটলিন

    moduleInstallClient
      .installModules(moduleInstallRequest)
      .addOnSuccessListener {
        if (it.areModulesAlreadyInstalled()) {
          // Modules are already installed when the request is sent.
        }
      }
      .addOnFailureListener {
        // Handle failure…
      }
    

    জাভা

    moduleInstallClient.installModules(moduleInstallRequest)
        .addOnSuccessListener(
            response -> {
              if (response.areModulesAlreadyInstalled()) {
                // Modules are already installed when the request is sent.
              }
            })
        .addOnFailureListener(
            e -> {
              // Handle failure...
            });
    

FakeModuleInstallClient এর সাথে স্থানীয় পরীক্ষা

Google Play পরিষেবা SDKs FakeModuleInstallClient প্রদান করে যাতে আপনি নির্ভরতা ইনজেকশন ব্যবহার করে পরীক্ষায় মডিউল ইনস্টল API-এর ফলাফল অনুকরণ করতে পারেন।

অ্যাপের পূর্বশর্ত

হিল্ট নির্ভরতা ইনজেকশন ফ্রেমওয়ার্ক ব্যবহার করতে আপনার অ্যাপ কনফিগার করুন।

পরীক্ষায় FakeModuleInstallClient দিয়ে ModuleInstallClient প্রতিস্থাপন করুন

  1. নির্ভরতা যোগ করুন:

    আপনার মডিউলের Gradle বিল্ড ফাইলে (সাধারণত app/build.gradle ), আপনার পরীক্ষায় play-services-base-testing জন্য Google Play পরিষেবা নির্ভরতা যোগ করুন।

      dependencies {
        // other dependencies...
    
        testImplementation 'com.google.android.gms:play-services-base-testing:16.0.0'
      }
    
  2. ModuleInstallClient প্রদান করতে একটি হিল্ট মডিউল তৈরি করুন:

    কোটলিন

    @Module
    @InstallIn(ActivityComponent::class)
    object ModuleInstallModule {
    
      @Provides
      fun provideModuleInstallClient(
        @ActivityContext context: Context
      ): ModuleInstallClient = ModuleInstall.getClient(context)
    }
    

    জাভা

    @Module
    @InstallIn(ActivityComponent.class)
    public class ModuleInstallModule {
      @Provides
      public static ModuleInstallClient provideModuleInstallClient(
        @ActivityContext Context context) {
        return ModuleInstall.getClient(context);
      }
    }
    
  3. কার্যকলাপে ModuleInstallClient ইনজেক্ট করুন:

    কোটলিন

    @AndroidEntryPoint
    class MyActivity: AppCompatActivity() {
      @Inject lateinit var moduleInstallClient: ModuleInstallClient
    
      ...
    }
    

    জাভা

    @AndroidEntryPoint
    public class MyActivity extends AppCompatActivity {
      @Inject ModuleInstallClient moduleInstallClient;
    
      ...
    }
    
  4. পরীক্ষায় বাঁধাই প্রতিস্থাপন করুন:

    কোটলিন

    @UninstallModules(ModuleInstallModule::class)
    @HiltAndroidTest
    class MyActivityTest {
      ...
      private val context:Context = ApplicationProvider.getApplicationContext()
      private val fakeModuleInstallClient = FakeModuleInstallClient(context)
      @BindValue @JvmField
      val moduleInstallClient: ModuleInstallClient = fakeModuleInstallClient
    
      ...
    }
    

    জাভা

    @UninstallModules(ModuleInstallModule.class)
    @HiltAndroidTest
    class MyActivityTest {
      ...
      private static final Context context = ApplicationProvider.getApplicationContext();
      private final FakeModuleInstallClient fakeModuleInstallClient = new FakeModuleInstallClient(context);
      @BindValue ModuleInstallClient moduleInstallClient = fakeModuleInstallClient;
    
      ...
    }
    

মডিউল প্রাপ্যতা অনুকরণ

কোটলিন

@Test
fun checkAvailability_available() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset()

  val availableModule = TfLite.getClient(context)
  fakeModuleInstallClient.setInstalledModules(api)

  // Verify the case where modules are already available...
}

@Test
fun checkAvailability_unavailable() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset()

  // Do not set any installed modules in the test.

  // Verify the case where modules unavailable on device...
}

@Test
fun checkAvailability_failed() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset()

  fakeModuleInstallClient.setModulesAvailabilityTask(Tasks.forException(RuntimeException()))

  // Verify the case where an RuntimeException happened when trying to get module's availability...
}

জাভা

@Test
public void checkAvailability_available() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset();

  OptionalModuleApi optionalModuleApi = TfLite.getClient(context);
  fakeModuleInstallClient.setInstalledModules(api);

  // Verify the case where modules are already available...
}

@Test
public void checkAvailability_unavailable() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset();

  // Do not set any installed modules in the test.

  // Verify the case where modules unavailable on device...
}

@Test
public void checkAvailability_failed() {
  fakeModuleInstallClient.setModulesAvailabilityTask(Tasks.forException(new RuntimeException()));

  // Verify the case where an RuntimeException happened when trying to get module's availability...
}

একটি বিলম্বিত ইনস্টল অনুরোধের জন্য ফলাফল অনুকরণ

কোটলিন

@Test
fun deferredInstall_success() {
  fakeModuleInstallClient.setDeferredInstallTask(Tasks.forResult(null))

  // Verify the case where the deferred install request has been sent successfully...
}

@Test
fun deferredInstall_failed() {
  fakeModuleInstallClient.setDeferredInstallTask(Tasks.forException(RuntimeException()))

  // Verify the case where an RuntimeException happened when trying to send the deferred install request...
}

জাভা

@Test
public void deferredInstall_success() {
  fakeModuleInstallClient.setDeferredInstallTask(Tasks.forResult(null));

  // Verify the case where the deferred install request has been sent successfully...
}

@Test
public void deferredInstall_failed() {
  fakeModuleInstallClient.setDeferredInstallTask(Tasks.forException(new RuntimeException()));

  // Verify the case where an RuntimeException happened when trying to send the deferred install request...
}

একটি জরুরী ইনস্টলেশন অনুরোধের জন্য ফলাফল অনুকরণ

কোটলিন

@Test
fun installModules_alreadyExist() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset();

  OptionalModuleApi optionalModuleApi = TfLite.getClient(context);
  fakeModuleInstallClient.setInstalledModules(api);

  // Verify the case where the modules already exist when sending the install request...
}

@Test
fun installModules_withoutListener() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset();

  // Verify the case where the urgent install request has been sent successfully...
}

@Test
fun installModules_withListener() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset();

  // Generates a ModuleInstallResponse and set it as the result for installModules().
  val moduleInstallResponse = FakeModuleInstallUtil.generateModuleInstallResponse()
  fakeModuleInstallClient.setInstallModulesTask(Tasks.forResult(moduleInstallResponse))

  // Verify the case where the urgent install request has been sent successfully...

  // Generates some fake ModuleInstallStatusUpdate and send it to listener.
  val update = FakeModuleInstallUtil.createModuleInstallStatusUpdate(
    moduleInstallResponse.sessionId, STATE_COMPLETED)
  fakeModuleInstallClient.sendInstallUpdates(listOf(update))

  // Verify the corresponding updates are handled correctly...
}

@Test
fun installModules_failed() {
  fakeModuleInstallClient.setInstallModulesTask(Tasks.forException(RuntimeException()))

  // Verify the case where an RuntimeException happened when trying to send the urgent install request...
}

জাভা

@Test
public void installModules_alreadyExist() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset();

  OptionalModuleApi optionalModuleApi = TfLite.getClient(context);
  fakeModuleInstallClient.setInstalledModules(api);

  // Verify the case where the modules already exist when sending the install request...
}

@Test
public void installModules_withoutListener() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset();

  // Verify the case where the urgent install request has been sent successfully...
}

@Test
public void installModules_withListener() {
  // Reset any previously installed modules.
  fakeModuleInstallClient.reset();

  // Generates a ModuleInstallResponse and set it as the result for installModules().
  ModuleInstallResponse moduleInstallResponse =
      FakeModuleInstallUtil.generateModuleInstallResponse();
  fakeModuleInstallClient.setInstallModulesTask(Tasks.forResult(moduleInstallResponse));

  // Verify the case where the urgent install request has been sent successfully...

  // Generates some fake ModuleInstallStatusUpdate and send it to listener.
  ModuleInstallStatusUpdate update = FakeModuleInstallUtil.createModuleInstallStatusUpdate(
      moduleInstallResponse.getSessionId(), STATE_COMPLETED);
  fakeModuleInstallClient.sendInstallUpdates(ImmutableList.of(update));

  // Verify the corresponding updates are handled correctly...
}

@Test
public void installModules_failed() {
  fakeModuleInstallClient.setInstallModulesTask(Tasks.forException(new RuntimeException()));

  // Verify the case where an RuntimeException happened when trying to send the urgent install request...
}