All Products
Search
Document Center

Mobile Platform as a Service:Register common components

Last Updated:May 22, 2023

A modular design method is one of the design principles of mPaaS framework. The low coupling and high cohesion of business modules are conducive to the expansion and maintenance of businesses.

Business modules exist in the form of Bundles and the modules do not affect each other. But there are some correlations between Bundles, such as jumping to another Bundle interface, calling APIs in another Bundle, or performing some operations in Bundle to be completed during initialization.

For this reason, mPaaS is designed with the metainfo general-purpose component registration mechanism, where each Bundle declares the components that need to be registered in metaInfo.xml.

The frameworks currently supports the following components:

  • ActivityApplication (Application)

  • ExternalService (Service)

  • BroadcastReceiver

  • Pipeline

The format of metainfo.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<metainfo>
    <broadcastReceiver>
        <className>com.mpaas.demo.broadcastreceiver.TestBroadcastReceiver</className>
        <action>com.mpaas.demo.broadcastreceiver.ACTION_TEST</action>
    </broadcastReceiver>
    <application>
        <className>com.mpaas.demo.activityapplication.MicroAppEntry</className>
        <appId>33330007</appId>
    </application>
</metainfo>

Application component

ActivityApplication is a component designed by the mPaaS framework and acts as an activity container. The ActivityApplication component allows you to manage and organize activities, specifically for solving the issue of jumping to another Bundle interface. Thus, the caller needs only to care about the ActivityApplication information registered in the framework on the business side and the agreed parameters.

About this task

A series of logic such as creation and destruction of ActivityApplication is completely managed by the mPaaS framework. The business side only needs to process the parameters it receives and manage the activities under its own business, so that the business side is effectively isolated from the caller. The business side and the caller only need to coordinate the invoked parameters, which reduces the dependency.

For Android native apps developed based on the mPaaS framework, activities must be inherited from BaseActivity or BaseFragmentActivity in order to be managed by the ActivityApplication class.

Procedure

  1. Create a metainfo.xml file in the main module of your project, and place it in the location as shown in the following figure:

  2. Write the following configurations into the metainfo.xml file, wherein:

    • className The configured class name is used to provide the class name to jump to and define the behavior of each stages. For class definitions, see codes in step three. The framework loads the corresponding class through the name defined by className. Therefore, the class must not be obfuscated and needs to be retained in the obfuscation file.

    • appId: Unique identifier of a business. The business side only needs to know the appId of the business to complete the jump. The mapping between appId and ActivityApplication is handled by the framework layer.

      <?xml version="1.0" encoding="UTF-8"?>
      <metainfo>
         <application>
             <className>com.mpaas.demo.hotpatch.HotpatchMicroApp</className>
             <appId>33330002</appId>
         </application>
      </metainfo>
  3. If the class specified by metainfo via className performs only simple jumps, the following code is used for implementation:

     /**
      * Scenario one:
      * If you can only jump to a certain activity interface, then you need to reload getEntryClassName and onRestart. For getEntryClassName, classname of the activity is returned, and for onRestart, getMicroApplicationContext().startActivity(this, getEntryClassName()) must be invoked;
      * Scenario two:
      * To jump to a different activity interface on demand, you need to reload onStart and onRestart, and jump to the specified interface based on the parameters in the Bundle project.
      * Created by mengfei on 2018/7/23.
      */
     public class MicroAppEntry extends ActivityApplication {
    
         @Override
         public String getEntryClassName() {
             //Scenario one: It is only possible to jump to a certain activity screen. In this case, classname is returned
             //Scenario two: Jumps to a certain interface according to parameters. The null result must be returned.
             return MainActivity.class.getName(); 
         }
    
         /**
          * Invoked during application creation; the implementation class can perform initialization here
          *
          * @param bundle
          */
         @Override
         protected void onCreate(Bundle bundle) {
             doStartApp(bundle);
         }
    
         /**
          * Invoked during application startup
          * If the application is not created yet, the create will be executed first, and then the onStart() callback
          */
         @Override
         protected void onStart() {
         }
    
         /**
          * When an application is destroyed, this callback function is invoked
          *
          * @param bundle
          */
         @Override
         protected void onDestroy(Bundle bundle) {
    
         }
    
         /**
          * During the application startup, if the application has been started, the onRestart() callback will be invoked instead of the onStart()
          *
          * @param bundle
          */
         @Override
         protected void onRestart(Bundle bundle) {
         //For scenario one: The getMicroApplicationContext().startActivity(this, getEntryClassName()) must be invoked here;
             doStartApp(bundle);
         }
    
         /**
          * When a new application is started, the current application will be paused, and the method is called back
          */
         @Override
         protected void onStop() {
    
         }
    
         private void doStartApp(Bundle bundle) {
             String dest = bundle.getString("dest");
             if ("main".equals(dest)) {
                 Context ctx = LauncherApplicationAgent.getInstance().getApplicationContext();
                 ctx.startActivity(new Intent(ctx, MainActivity.class));
             } else if ("second".equals(dest)) {
                 Context ctx = LauncherApplicationAgent.getInstance().getApplicationContext();
                 ctx.startActivity(new Intent(ctx, SecondActivity.class));
             }
         }
     }
  4. As the caller, you need to jump through the API provided in the MicroApplicationContext encapsulated by the framework. curId or specify null for the parameter:

     // Gets the MicroApplicationContext object:
     MicroApplicationContext context = MPFramework.getMicroApplicationContext();
     String curId = "";
     ActivityApplication curApp = context.getTopApplication();
      if (null != curApp) {
          curId = curApp.getAppId();
     }
     String appId = "ID of destination ApplicationActivity";
     Bundle bundle = new Bundle(); // Additional parameter. This parameter is not mandatory for passing.
     context.startApp(curId, appId, bundle);

Service component

mPaaS is designed with a service component to address the issue with invoking APIs across Bundles. The service component will be used to provide some logic as a service for being used by other modules.

About this task

The service component has the following features:

  • There is no constraint to UI.

  • The API is separated from implementation in design.

In principle, only the API classes are visible to callers. Therefore, the API classes must be defined in the API module. The implementation must be defined in the main module. Note that by default, an API module named api is generated when building a Bundle project.

External invocations are made through the findServiceByInterface API of MicroApplicationContext to get the corresponding service through interfaceName. For the use of Bundle, only the service abstract API classes, i.e. those defined in interfaceName, are exposed. Abstract API classes are defined in the API package.

Procedure

Register the service component in the following steps:

  1. Define the location of metainfo.xml, as shown in the following figure:

  2. Write the following configurations into the metainfo.xml file. The framework uses interfaceName as key, and className as value, and records the mapping relationship between them. Of them, className is the implementation class of an API, and interfaceName is the abstract API class:

    <metainfo>
     <service>
         <className>com.mpaas.cq.bundleb.MyServiceImpl</className>
         <interfaceName>com.mpaas.cq.bundleb.api.MyService</interfaceName>
         <isLazy>true</isLazy>
     </service>
    </metainfo>
    • An abstract API class is defined as follows:

      public abstract class MyService extends ExternalService {
      public abstract String funA();
      }
    • An API class implementation is defined as follows:

      public class MyServiceImpl extends MyService {
        @Override
        public String funA() {
            return "This is the API by service which is provided by BundleB";
        }
      
        @Override
        protected void onCreate(Bundle bundle) {
      
        }
      
        @Override
        protected void onDestroy(Bundle bundle) {
      
        }
      }
    • An external invocation method is defined as follows:

      MyService myservice = LauncherApplicationAgent.getInstance().getMicroApplicationContext().findServiceByInterface(MyService.class.getName());
      myservice.funA();

BroadcastReceiver component

BroadcastReceiver is the encapsulation of android.content.BroadcastReceiver, but the difference is that the mPaaS framework uses android.support.v4.content.LocalBroadcastManager to register and unregister BroadcastReciever. Therefore, these broadcasts are only used internally within the current application, and in addition, the mPaaS framework is built with a series of broadcast events for being monitored by users.

mPaaS built-in broadcast events

mPaaS defines multiple broadcast events that are primarily used to monitor the states of the current application. The registration of a listener is no different from that in a native development environment. But note that these states can only be monitored by the host process. The sample code is as follows:

Sample code

The built-in broadcast events are as follows:

public interface MsgCodeConstants {
    String FRAMEWORK_ACTIVITY_CREATE = "com.alipay.mobile.framework.ACTIVITY_CREATE";
    String FRAMEWORK_ACTIVITY_RESUME = "com.alipay.mobile.framework.ACTIVITY_RESUME";
    String FRAMEWORK_ACTIVITY_PAUSE = "com.alipay.mobile.framework.ACTIVITY_PAUSE";
    //Broadcast indicating that a user logs off, switch-to-backend broadcast
    String FRAMEWORK_ACTIVITY_USERLEAVEHINT = "com.alipay.mobile.framework.USERLEAVEHINT";
    //Broadcast indicating that all activities stop. This may be the switch-to-backend broadcast, but no the same judgment logic applies now
    String FRAMEWORK_ACTIVITY_ALL_STOPPED = "com.alipay.mobile.framework.ACTIVITY_ALL_STOPPED";
    String FRAMEWORK_WINDOW_FOCUS_CHANGED = "com.alipay.mobile.framework.WINDOW_FOCUS_CHANGED";
    String FRAMEWORK_ACTIVITY_DESTROY = "com.alipay.mobile.framework.ACTIVITY_DESTROY";
    String FRAMEWORK_ACTIVITY_START = "com.alipay.mobile.framework.ACTIVITY_START";
    String FRAMEWORK_ACTIVITY_DATA = "com.alipay.mobile.framework.ACTIVITY_DATA";
    String FRAMEWORK_APP_DATA = "com.alipay.mobile.framework.APP_DATA";
    String FRAMEWORK_IS_TINY_APP = "com.alipay.mobile.framework.IS_TINY_APP";
    String FRAMEWORK_IS_RN_APP = "com.alipay.mobile.framework.IS_RN_APP";
    //Broadcast indicating that a user returns to the front-end
    String FRAMEWORK_BROUGHT_TO_FOREGROUND = "com.alipay.mobile.framework.BROUGHT_TO_FOREGROUND";
}

Customize broadcast events

  1. Define the location of metainfo.xml, as shown in the following figure:

  2. Write the following configurations into the metainfo.xml file:

     <?xml version="1.0" encoding="UTF-8"?>
     <metainfo>
         <broadcastReceiver>
             <className>com.mpaas.demo.broadcastreceiver.TestBroadcastReceiver</className>
             <action>com.mpaas.demo.broadcastreceiver.ACTION_TEST</action>
         </broadcastReceiver>
     </metainfo>
    • Customize Receiver implementation

      public class TestBroadcastReceiver extends BroadcastReceiver {
         private static final String ACTION_TEST = "com.mpaas.demo.broadcastreceiver.ACTION_TEST";
      
         @Override
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
             if (ACTION_TEST.equals(action)) {
                 //TODO
             }
         }
      }
    • Send broadcast

      LocalBroadcastManager.getInstance(LauncherApplicationAgent.getInstance().getApplicationContext()).sendBroadcast(new Intent("com.mpaas.demo.broadcastreceiver.ACTION_TEST"));

Pipeline component

The mPaaS framework has an obvious startup process. The pipeline mechanism allows the business line to encapsulate its own run logic into runnable and then place it in the pipeline. The framework starts an appropriate pipeline at an appropriate stage.

The following defines the pipeline timing:

  • com.alipay.mobile.framework.INITED: The framework is initialized. The framework can also be initialized when the process starts in the background.

  • com.alipay.mobile.client.STARTED: The client starts initialization. You have to wait until a page appears, for example, the welcome page.

  • com.alipay.mobile.TASK_SCHEDULE_SERVICE_IDLE_TASK: Lowest priority. This is executed only when there are no other operations with higher priority

As the Pipeline invocation is triggered by the framework, the user only needs to specify the appropriate timing in metaInfo.

Procedure

  1. Define the location of metainfo.xml, as shown in the following figure:

  2. Write the following configurations into the metainfo.xml file:

     <?xml version="1.0" encoding="UTF-8"?>
     <metainfo>
         <valve>
             <className>com.mpaas.demo.pipeline.TestPipeLine</className>
             <!--pipelineName is used to specify the stage at which execution occurs-->
             <pipelineName>com.alipay.mobile.client.STARTED</pipelineName>
             <threadName>com.mpaas.demo.pipeline.TestPipeLine</threadName>
             <!--weight is used to specify the priority of an operation. The lower the value is, the higher the execution priority is-->
             <weight>10</weight>
         </valve>
     </metainfo>
  3. To implement the pipeline:

     public class TestPipeLine implements Runnable {
         @Override
         public void run() {
            //...
         }
     }