Skip to content

Commit d7e605f

Browse files
author
Gabriel-Codrin Cojocaru
committed
Android TV launcher and lock task fixes
Add a launcher intent filter for Android TV. Use the LEANBACK_LAUNCHER category instead of the LAUNCHER category when creating launcher intents on Android TV. When starting apps from the kiosk mode activity use getLeanbackLaunchIntentForPackage instead of getLaunchIntentForPackage when running on Android TV. Test: Manual testing Bug: 161220655 Change-Id: Id5cf498bfd7aa9967e57abe2451e85181740612c
1 parent b6e4c43 commit d7e605f

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
3232
<uses-permission android:name="android.permission.REQUEST_PASSWORD_COMPLEXITY"/>
3333
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
34+
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
3435

3536
<application
3637
android:allowBackup="true"
3738
android:icon="@drawable/ic_launcher"
39+
android:banner="@drawable/ic_launcher"
3840
android:theme="@style/AppTheme"
3941
android:label="@string/app_name">
4042

@@ -47,6 +49,10 @@
4749
<action android:name="android.intent.action.MAIN"/>
4850
<category android:name="android.intent.category.LAUNCHER"/>
4951
</intent-filter>
52+
<intent-filter>
53+
<action android:name="android.intent.action.MAIN"/>
54+
<category android:name="android.intent.category.LEANBACK_LAUNCHER"/>
55+
</intent-filter>
5056
<intent-filter>
5157
<action android:name="android.app.action.CHECK_POLICY_COMPLIANCE"/>
5258
<category android:name="android.intent.category.DEFAULT"/>

app/src/main/java/com/afwsamples/testdpc/common/Util.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
import android.annotation.TargetApi;
2020
import android.app.Service;
21+
import android.app.UiModeManager;
2122
import android.app.admin.DevicePolicyManager;
2223
import android.content.ActivityNotFoundException;
2324
import android.content.ComponentName;
2425
import android.content.Context;
2526
import android.content.Intent;
2627
import android.content.IntentFilter;
28+
import android.content.res.Configuration;
2729
import android.graphics.BitmapFactory;
2830
import android.net.Uri;
2931
import android.os.Build.VERSION;
@@ -255,6 +257,17 @@ public static IntentFilter getHomeIntentFilter() {
255257
return filter;
256258
}
257259

260+
/** @return Intent for a launcher activity */
261+
public static Intent getLauncherIntent(Context context) {
262+
Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
263+
if (Util.isRunningOnTvDevice(context)) {
264+
launcherIntent.addCategory(Intent.CATEGORY_LEANBACK_LAUNCHER);
265+
} else {
266+
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
267+
}
268+
return launcherIntent;
269+
}
270+
258271
private static DevicePolicyManager getDevicePolicyManager(Context context) {
259272
return (DevicePolicyManager)context.getSystemService(Service.DEVICE_POLICY_SERVICE);
260273
}
@@ -266,4 +279,9 @@ public static boolean hasDelegation(Context context, String delegation) {
266279
DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
267280
return dpm.getDelegatedScopes(null, context.getPackageName()).contains(delegation);
268281
}
282+
283+
public static boolean isRunningOnTvDevice(Context context) {
284+
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
285+
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
286+
}
269287
}

app/src/main/java/com/afwsamples/testdpc/policy/PolicyManagementFragment.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,8 +1666,7 @@ private void showManageLockTaskListPrompt(int dialogTitle,
16661666
if (getActivity() == null || getActivity().isFinishing()) {
16671667
return;
16681668
}
1669-
Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
1670-
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1669+
Intent launcherIntent = Util.getLauncherIntent(getActivity());
16711670
final List<ResolveInfo> primaryUserAppList = mPackageManager
16721671
.queryIntentActivities(launcherIntent, 0);
16731672
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
@@ -3396,8 +3395,7 @@ private boolean isPackageSuspended(String packageName) {
33963395
}
33973396

33983397
private List<ResolveInfo> getAllLauncherIntentResolversSorted() {
3399-
final Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
3400-
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
3398+
final Intent launcherIntent = Util.getLauncherIntent(getActivity());
34013399
final List<ResolveInfo> launcherIntentResolvers = mPackageManager
34023400
.queryIntentActivities(launcherIntent, 0);
34033401
Collections.sort(launcherIntentResolvers,

app/src/main/java/com/afwsamples/testdpc/policy/locktask/KioskModeActivity.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,15 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
269269
return;
270270
}
271271
PackageManager pm = getPackageManager();
272-
startActivity(pm.getLaunchIntentForPackage(getItem(position)));
272+
Intent launchAppIntent;
273+
String appPackage = getItem(position);
274+
275+
if (Util.isRunningOnTvDevice(getContext())) {
276+
launchAppIntent = pm.getLeanbackLaunchIntentForPackage(appPackage);
277+
} else {
278+
launchAppIntent = pm.getLaunchIntentForPackage(appPackage);
279+
}
280+
startActivity(launchAppIntent);
273281
}
274282
}
275283
}

0 commit comments

Comments
 (0)