ข้อมูลหน้าจอหลักของ Android มาจากคลาส LauncherProvider ซึ่ง
ขยาย ContentProvider ทำให้สามารถนำเข้าและส่งออกข้อมูลพื้นที่ทำงานของ Launcher
โดยใช้ XML ได้
โต้ตอบกับผู้ให้บริการเนื้อหา
หากต้องการโต้ตอบกับคลาส LaunchProvider ซึ่งขยาย ContentProvider
ให้ใช้วิธี call ดังนี้
class LauncherProvider : ContentProvider {
public Bundle call(String method, String arg, Bundle extras);
}
เรียกใช้คลาส LaunchProvider
หากต้องการเรียกใช้คลาส LauncherProvider จากแอป ให้ใช้คำสั่งต่อไปนี้
class YourClass {
/**
* This method imports Launcher workspace data as a XML string. Calling this method clears the old
* data model within Launcher and replaces it with the imported data. It should be noted
* that it doesn't need to clear all the Launcher's data, just what is similar to what is being imported.
*/
fun importLauncherData(xmlRepresentation: String, ctx: Context): Boolean {
val uri = try {
getLauncherProviderUri(ctx)
} catch (e: IllegalStateException) {
Log.e(TAG, "Failed to get launcher provider URI", e)
return false
}
val bundle = ctx.contentResolver.call(
uri,
LauncherProviderConstants.METHOD_IMPORT_LAYOUT_XML,
xmlRepresentation,
null,
)
return LauncherProviderConstants.SUCCESS
.equals(bundle.getBoolean(LauncherProviderConstants.KEY_RESULT))
}
/**
* Use this function to retrieve an XML string representation of the Launcher's Workspace.
* This method doesn't return what the user sees on their home screen,
* but rather what is in their data model at the moment it's called.
*/
fun exportLauncherData(xmlRepresentation: String, ctx: Context): String {
val uri = try {
getLauncherProviderUri(ctx)
} catch (e: IllegalStateException) {
Log.e(TAG, "Failed to get launcher provider URI", e)
return ""
}
val bundle = ctx.contentResolver.call(
uri,
LauncherProviderConstants.METHOD_EXPORT_LAYOUT_XML,
null,
null,
)
if (LauncherProviderConstants.FAILURE
.equals(bundle.getBoolean(LauncherProviderConstants.KEY_RESULT))) {
Log.e(TAG, "failed to export launcher data; review previous logs for the cause.")
}
return bundle.getString(LauncherProviderConstants.KEY_LAYOUT, "")
}
/**
* Returns a Uri for interacting with Launcher's ContentProvider.
*
* Not all Launchers implement this api. This method throws an IllegalStateException
* if the Launcher doesn't support it.
*/
private fun getLauncherProviderUri(ctx: Context): Uri {
val homeIntent = Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME)
val launcherPackage: String =
ctx.packageManager
.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY)
?.activityInfo
?.packageName ?: throw IllegalStateException("No launcher package found")
val authority = "${launcherPackage}.settings"
ctx.packageManager.resolveContentProvider(authority, 0)
?: throw IllegalStateException(
"Launcher package '$launcherPackage' does not support LauncherProvider",
)
return "content://$authority".toUri()
}
}
พารามิเตอร์
ค่าคงที่เหล่านี้ใช้ในเมธอดการเรียก
object LauncherProviderConstants {
// Valid arg parameters for export and import operations
private static final String METHOD_EXPORT_LAYOUT_XML = "EXPORT_LAYOUT_XML";
private static final String METHOD_IMPORT_LAYOUT_XML = "IMPORT_LAYOUT_XML";
// Bundle key and value set for determining if operation completed successfully or not
private static final String KEY_RESULT = "KEY_RESULT";
private static final String SUCCESS = "success";
private static final String FAILURE = "failure";
// Bundle key used to store exported XML-string representation of Launcher's workspace layout
// and item metadata
private static final String KEY_LAYOUT = "KEY_LAYOUT";
}
ใช้ค่าคงที่ LauncherProvider โดยมีข้อจำกัดต่อไปนี้
- ใช้เมธอด
contentResolver.callกับEXPORT_LAYOUT_XMLเป็นพารามิเตอร์เมธอด เพื่อส่งออกการแสดง XML ของพื้นที่ทํางานของ Launcher - ในระหว่างการส่งออก คุณจะเข้าถึงการแสดง XML ได้ในแพ็กเกจที่ส่งคืนโดยใช้คีย์
KEY_LAYOUT - ใช้เมธอด
contentResolver.callกับIMPORT_LAYOUT_XMLเป็นพารามิเตอร์เมธอด เพื่อนำเข้าการแสดง XML ของพื้นที่ทำงานของ Launcher - ในระหว่างการนำเข้า ระบบจะระบุการแสดง XML เป็น
argพารามิเตอร์ของเมธอดการเรียก - สําหรับการเรียก API ทั้งการส่งออกและการนําเข้า การดําเนินการที่เลือกจะเสร็จสมบูรณ์และสําเร็จจะแสดง
successและการดําเนินการที่ถูกขัดจังหวะหรือล้มเหลวจะแสดงfailure - คุณเรียกค่า
successหรือfailureในแพ็กเกจที่ส่งคืนได้โดยใช้คีย์KEY_RESULT
ดูตัวอย่างได้ที่เรียกใช้คลาส LaunchProvider
การแสดง XML
ใช้คำแนะนำต่อไปนี้สำหรับโครงสร้าง XML ระหว่างการนำเข้าและส่งออก
แอป Workspace
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <workspace rows="4" columns="5"> <autoinstall container="desktop" x="1" y="1" screen="0" className="com.android.launcher3.tests.Activity2" packageName="com.google.android.apps.nexuslauncher" /> </workspace>แอป Hotseat
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <workspace> <autoinstall container="hotseat" rank="0" className="com.android.launcher3.tests.Activity2" packageName="com.google.android.apps.nexuslauncher" /> </workspace>วิดเจ็ต
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <workspace> <appwidget container="desktop" spanX="2" spanY="2" x="0" y="1" screen="0" className="PlaceholderWidget" packageName="com.test.pending" /> </workspace>โฟลเดอร์:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <workspace> <folder container="desktop" x="1" y="1" screen="1" titleText="CustomFolder"> <autoinstall className="com.android.launcher3.tests.Activity1" packageName="com.google.android.apps.nexuslauncher" /> <autoinstall className="com.android.launcher3.tests.Activity2" packageName="com.google.android.apps.nexuslauncher" /> <autoinstall className="com.android.launcher3.tests.Activity3" packageName="com.google.android.apps.nexuslauncher" /> </folder> </workspace>ทางลัดแบบลึก
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <workspace> <shortcut shortcutId="shortcut2" packageName="com.google.android.apps.nexuslauncher.tests" /> </workspace>คู่แอป:
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <workspace> <apppair container="desktop" x="1" y="1" screen="1" titleText="CustomFolder"> <autoinstall className="com.android.launcher3.tests.Activity1" packageName="com.google.android.apps.nexuslauncher" /> <autoinstall className="com.android.launcher3.tests.Activity2" packageName="com.google.android.apps.nexuslauncher" /> </apppair> </workspace>
สมมติฐานด้านพฤติกรรม
สมมติฐานด้านพฤติกรรมสำหรับคลาส LaunchProvider มีดังนี้
- เมธอดเป็นแบบอะตอมและบล็อก
- ระบบจะเขียนทับข้อมูลที่คล้ายกันในตัวเรียกใช้ระหว่างการนำเข้า ทำให้เหลือเพียง ข้อมูลที่นำเข้าใหม่เท่านั้น
- คุณจะเข้าถึงข้อมูลที่นําเข้าได้ทันที และการส่งออกหลังจากนําเข้า จะแสดงข้อมูลใหม่