Android 홈 화면 데이터는 ContentProvider를 확장하는 LauncherProvider 클래스에서 제공하므로 XML을 사용하여 런처 작업공간 데이터를 가져오고 내보낼 수 있습니다.
콘텐츠 제공업체와 상호작용
ContentProvider을 확장하는 LaunchProvider 클래스와 상호작용하려면 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 상수를 사용합니다.
EXPORT_LAYOUT_XML을 메서드 매개변수로 사용하여contentResolver.call메서드를 사용하여 런처의 작업공간 XML 표현을 내보냅니다.- 내보내기 중에 XML 표현은
KEY_LAYOUT키를 사용하여 반환된 번들에서 액세스할 수 있습니다. IMPORT_LAYOUT_XML을 메서드 매개변수로 사용하여contentResolver.call메서드를 사용하여 런처의 작업공간 XML 표현을 가져옵니다.- 가져오기 중에 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>즐겨찾는 앱 모음 앱:
<?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 클래스의 동작 가정입니다.
- 메서드는 원자적이며 차단됩니다.
- 런처의 유사한 데이터는 가져오는 동안 덮어쓰여 새로 가져온 데이터만 남습니다.
- 가져온 데이터는 즉시 액세스할 수 있으며, 가져온 직후에 내보내면 새 데이터가 반환됩니다.