Simple analysis of DocumentUI in Android

Posted by jonorr1 on Tue, 11 Jan 2022 15:59:58 +0100

DocumentUI process analysis

DocumentUI is a document manager. In our daily use, in addition to a separate entry, we enter it more by selecting documents through other applications.
In the source code of Android 12, the Mainfest file also provides us with two methods to enter activities through LauncherActivity and FilesActivity. The specific code is as follows:

 <activity-alias
100             android:name=".LauncherActivity"
101             android:targetActivity=".files.LauncherActivity"
102             android:label="@string/launcher_label"
103             android:exported="true"
104             android:icon="@drawable/launcher_icon" >
105             <intent-filter>
106                 <action android:name="android.intent.action.MAIN" />
107                 <category android:name="android.intent.category.LAUNCHER" />
108                 <category android:name="android.intent.category.APP_FILES" />
109             </intent-filter>
110             <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />
111         </activity-alias>
112 
113         <activity
114             android:name=".files.FilesActivity"
115             android:documentLaunchMode="intoExisting"
116             android:exported="true"
117             android:theme="@style/LauncherTheme">
118             <intent-filter>
119                 <action android:name="android.intent.action.MAIN" />
120             </intent-filter>
121             <intent-filter>
122                 <action android:name="android.intent.action.VIEW" />
123                 <category android:name="android.intent.category.DEFAULT" />
124                 <data android:mimeType="vnd.android.document/root" />
125             </intent-filter>
126             <intent-filter>
127                 <action android:name="android.intent.action.VIEW" />
128                 <category android:name="android.intent.category.DEFAULT" />
129                 <data android:mimeType="vnd.android.document/directory" />
130             </intent-filter>
131         </activity>

Android is declared in LauncherActivity intent. category. Launcher can be displayed in the program list. This time, we will mainly learn the DocumentUI process through the LauncherActivity entry.
In the onCreate() method in the LauncherActivity, the operation is mainly carried out through the Launcher() method. The specific code is as follows:

 protected void onCreate(Bundle savedInstanceState) {
61          super.onCreate(savedInstanceState);
63          launch();
65          finish();
66      }
68      private void launch() {
69          ActivityManager activities = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
71          Intent intent = findTask(activities);
72          if (intent != null) {
73              if (restoreTask(intent)) {
74                  return;
75              } else {
79                  clearTask(activities);
80              }
81          }
83          startTask();
84      }

In the launch() method, first obtain the system service and check whether there are tasks to be executed through the findTask () method. If there are restoreTask () recovery tasks, execute startActivity(imtent) to jump to the activity. Otherwise, execute clearTask to complete and eliminate the tasks. After that, execute the StartTask () method. In this method, transfer the createLaunchIntent to activities, and then start startActivities:

 public static final Intent createLaunchIntent(Activity activity) {
137          Intent intent = new Intent(activity, FilesActivity.class);
138          intent.setData(buildLaunchUri());
141          Intent original = activity.getIntent();
142          if (original != null) {
143              copyExtras(original, intent);
144              if (original.hasExtra(Intent.EXTRA_TITLE)) {
145                  intent.putExtra(
146                          Intent.EXTRA_TITLE,
147                          original.getStringExtra(Intent.EXTRA_TITLE));
148              }
149          }
150          return intent;
151      }

In createLauncherIntent, first jump to FilesActivity and operate the data in URI format through setData(). After viewing in Mainfest file, you can know that the action attribute is VIEW, which is used to

<action android:name="android.intent.action.VIEW" />

User display data. Get specific information in bulidLaunchUri() to access the data. Set the original and temporarily store the data to be transferred in the original through the putExtra () method. After another activity is started, you only need to take these data out of the original.
Finally, go to the finish () method.
This is just a process in the DocumentUI according to the LauncherActivity code. In version 12, the Job class is set in the Service to compress, delete and move files. In the base, some basic information such as documentinfo, doucumentstack and display are defined. In the documentinfo, userId, authority, documentId, mimeType, displayName lastModified, flags, summary, size, icon and other parameters, and use Linklist to store the file list. More knowledge about DocumentUI needs to be updated, such as database, and will be updated continuously in the future.

Topics: Java Android Android Studio