Class Power


  • public class Power
    extends ApiBindBase
    This class provides power related APIs.

    Java Sample Code:

      package com.mitac.api;
    
      import android.app.Activity;
      import android.os.Bundle;
    
      import com.mitac.api.libs.Power;
      import com.mitac.api.libs.ServiceStatusCallback;
    
      public class MainActivity extends Activity {
          private Power mPower;
    
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
    
              // when creating the instance the service is auto binding
              // register a callback to listen service status
              mPower = new Power(getApplicationContext(), new ServiceStatusCallback() {
                  @Override
                  public void ready() {
                      startTest();
                  }
              });
          }
    
          private void startTest() {
              if (mPower != null) {
                  if (!mPower.setEnableCradle5vPower(true)) { // enable power
                      // remote fail to do the operation
                  }
    
                  if (!mPower.shutdown(false)) {
                      // remote fail to do the operation
                  }
    
                  if (mPower.isCradleInputPower5V()) {
                      // cradle input power 5v
                  }
    
                  if (mPower.isCradleInputPower12V()) {
                      // cradle input power 12v
                  }
    
                  Log.d("PowerTest", "cradle input power status: " + mPower.getCradleInputPowerStatus());
              }
          }
    
          @Override
          protected void onStop() {
              super.onStop();
    
              if (mPower != null) {
                  mPower.unbindService();
              }
          }
      }
     

    Kotlin Sample Code:

     package com.mitac.kotlinapitest
    
     import android.content.Context
     import androidx.appcompat.app.AppCompatActivity
     import android.os.Bundle
     import android.util.Log
     import com.mitac.api.libs.Power
     import com.mitac.api.libs.ServiceStatusCallback
    
     class MainActivity : AppCompatActivity() {
         val TAG: String = "PowerAPI"
         var power: Power? = null
    
         override fun onCreate(savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)
             setContentView(R.layout.activity_main)
             testPowerApi(applicationContext)
         }
    
         fun testPowerApi(context: Context) {
             power = Power(context, object: ServiceStatusCallback {
                 override fun ready() {
                     Log.d(TAG, "service is ready")
                     testPowerObject(power!!)
                 }
    
                 override fun stopped() {
                     Log.d(TAG, "service is stopped")
                 }
             })
         }
    
         fun testPowerObject(powerObj: Power) {
             powerObj!!.reboot(false)
         }
     }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      Power​(android.content.Context context)
      This constructor will not bind the service automatically.
      Power​(android.content.Context context, ServiceStatusCallback callback)
      This constructor will bind the service automatically.
    • Constructor Detail

      • Power

        public Power​(android.content.Context context)
        This constructor will not bind the service automatically. Please call bindService() at least once before using APIs.
        Parameters:
        context - better to use Context.getApplicationContext() to prevent memory leaks
        Throws:
        java.lang.IllegalArgumentException - if context is null.
      • Power

        public Power​(android.content.Context context,
                     ServiceStatusCallback callback)
        This constructor will bind the service automatically.
        Parameters:
        context - better to use Context.getApplicationContext() to prevent memory leaks
        callback - used to get the notification when service is ready.
        Throws:
        java.lang.IllegalArgumentException - if context or callback is null.
        See Also:
        ServiceStatusCallback
    • Method Detail

      • setEnableCradle5vPower

        public boolean setEnableCradle5vPower​(boolean enable)
        Sets cradle 5v power enable or disable
        Parameters:
        enable - if true enable cradle 5v power; false otherwise.
        Returns:
        false if remote fail.
      • shutdown

        public boolean shutdown​(boolean confirm)
        Shut down device
        Parameters:
        confirm - if true show the confirm dialog; false shut down the device immediately
        Returns:
        false if remote fail.
      • reboot

        public boolean reboot​(boolean confirm)
        Reboot device
        Parameters:
        confirm - if true show the confirm dialog; false reboot the device immediately
        Returns:
        false if remote fail.
      • goToSleep

        public boolean goToSleep()
        Suspend the device
        Returns:
        false if remote fail.
      • isCradleInputPower5V

        public boolean isCradleInputPower5V()
        Checks if current cradle input power source is 5V
        Returns:
        true if power source is from 5V; false otherwise
      • isCradleInputPower12V

        public boolean isCradleInputPower12V()
        Checks if current cradle input power source is 12V
        Returns:
        true if power source is from 12V; false otherwise
      • getCradleInputPowerStatus

        public java.lang.String getCradleInputPowerStatus()
        Gets current cradle input power status.
        Returns:
        current power source status; null if remote fail.
        See Also:
        for register cradle ACC off event
      • isServiceReady

        public boolean isServiceReady()
        Check if service is ready.
        Returns:
        true if ready; false otherwise.