Android のホーム画面データは LauncherProvider クラスによって提供されます。このクラスは ContentProvider を拡張し、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()
    }
}
パラメータ
これらの定数は、call メソッドで使用されます。
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キーを使用して返されたバンドルでアクセスできます。
- contentResolver.callメソッドで- IMPORT_LAYOUT_XMLをメソッド パラメータとして使用して、ランチャーのワークスペースの 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 クラスの動作に関する前提条件は次のとおりです。
- メソッドはアトミックで、ブロッキングです。
- ランチャー内の類似データはインポート時に上書きされ、新しくインポートされたデータのみが残ります。
- インポートされたデータにはすぐにアクセスできます。インポート直後にエクスポートすると、新しいデータが返されます。
