Refactor shortcut and channel code and handle removal of apps and PCs properly

This commit is contained in:
Cameron Gutman
2019-07-16 22:16:29 -07:00
parent dc984e8679
commit d54fdc9f5f
5 changed files with 113 additions and 61 deletions

View File

@@ -82,44 +82,44 @@ public class ShortcutHelper {
return false;
}
public void reportShortcutUsed(String computerUuid) {
public void reportComputerShortcutUsed(ComputerDetails computer) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
if (getInfoForId(computerUuid) != null) {
sm.reportShortcutUsed(computerUuid);
if (getInfoForId(computer.uuid) != null) {
sm.reportShortcutUsed(computer.uuid);
}
}
}
public void reportGameLaunched(String computerUuid, String computerName, String appId, String appName) {
tvChannelHelper.createTvChannel(computerUuid, computerName);
tvChannelHelper.addGameToChannel(computerUuid, computerName, appId, appName);
public void reportGameLaunched(ComputerDetails computer, NvApp app) {
tvChannelHelper.createTvChannel(computer);
tvChannelHelper.addGameToChannel(computer, app);
}
public void createAppViewShortcut(String id, String computerName, String computerUuid, boolean forceAdd, boolean newlyPaired) {
public void createAppViewShortcut(ComputerDetails computer, boolean forceAdd, boolean newlyPaired) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
Intent i = new Intent(context, ShortcutTrampoline.class);
i.putExtra(AppView.NAME_EXTRA, computerName);
i.putExtra(AppView.UUID_EXTRA, computerUuid);
i.putExtra(AppView.NAME_EXTRA, computer.name);
i.putExtra(AppView.UUID_EXTRA, computer.uuid);
i.setAction(Intent.ACTION_DEFAULT);
ShortcutInfo sinfo = new ShortcutInfo.Builder(context, id)
ShortcutInfo sinfo = new ShortcutInfo.Builder(context, computer.uuid)
.setIntent(i)
.setShortLabel(computerName)
.setLongLabel(computerName)
.setShortLabel(computer.name)
.setLongLabel(computer.name)
.setIcon(Icon.createWithResource(context, R.mipmap.ic_pc_scut))
.build();
ShortcutInfo existingSinfo = getInfoForId(id);
ShortcutInfo existingSinfo = getInfoForId(computer.uuid);
if (existingSinfo != null) {
// Update in place
sm.updateShortcuts(Collections.singletonList(sinfo));
sm.enableShortcuts(Collections.singletonList(id));
sm.enableShortcuts(Collections.singletonList(computer.uuid));
}
// Reap shortcuts to make space for this if it's new
// NOTE: This CAN'T be an else on the above if, because it's
// possible that we have an existing shortcut but it's not a dynamic one.
if (!isExistingDynamicShortcut(id)) {
if (!isExistingDynamicShortcut(computer.uuid)) {
// To avoid a random carousel of shortcuts popping in and out based on polling status,
// we only add shortcuts if it's not at the limit or the user made a conscious action
// to interact with this PC.
@@ -132,24 +132,28 @@ public class ShortcutHelper {
if (newlyPaired) {
// Avoid hammering the channel API for each computer poll because it will throttle us
tvChannelHelper.createTvChannel(computerUuid, computerName);
tvChannelHelper.requestChannelOnHomeScreen(computerUuid);
tvChannelHelper.createTvChannel(computer);
tvChannelHelper.requestChannelOnHomeScreen(computer);
}
}
public void createAppViewShortcutForOnlineHost(ComputerDetails details) {
createAppViewShortcut(details.uuid, details.name, details.uuid, false, false);
createAppViewShortcut(details, false, false);
}
private String getShortcutIdForGame(ComputerDetails computer, NvApp app) {
return computer.uuid + app.getAppId();
}
@TargetApi(Build.VERSION_CODES.O)
public boolean createPinnedGameShortcut(String id, Bitmap iconBits, String computerName, String computerUuid, String appName, String appId) {
public boolean createPinnedGameShortcut(ComputerDetails computer, NvApp app, Bitmap iconBits) {
if (sm.isRequestPinShortcutSupported()) {
Icon appIcon;
Intent i = new Intent(context, ShortcutTrampoline.class);
i.putExtra(AppView.NAME_EXTRA, computerName);
i.putExtra(AppView.UUID_EXTRA, computerUuid);
i.putExtra(ShortcutTrampoline.APP_ID_EXTRA, appId);
i.putExtra(AppView.NAME_EXTRA, computer.name);
i.putExtra(AppView.UUID_EXTRA, computer.uuid);
i.putExtra(ShortcutTrampoline.APP_ID_EXTRA, ""+app.getAppId());
i.setAction(Intent.ACTION_DEFAULT);
if (iconBits != null) {
@@ -158,9 +162,9 @@ public class ShortcutHelper {
appIcon = Icon.createWithResource(context, R.mipmap.ic_pc_scut);
}
ShortcutInfo sInfo = new ShortcutInfo.Builder(context, id)
ShortcutInfo sInfo = new ShortcutInfo.Builder(context, getShortcutIdForGame(computer, app))
.setIntent(i)
.setShortLabel(appName + " (" + computerName + ")")
.setShortLabel(app.getAppName() + " (" + computer.name + ")")
.setIcon(appIcon)
.build();
@@ -170,15 +174,32 @@ public class ShortcutHelper {
}
}
public boolean createPinnedGameShortcut(String id, Bitmap iconBits, ComputerDetails cDetails, NvApp app) {
return createPinnedGameShortcut(id, iconBits, cDetails.name, cDetails.uuid, app.getAppName(), Integer.valueOf(app.getAppId()).toString());
public void disableComputerShortcut(ComputerDetails computer, CharSequence reason) {
tvChannelHelper.deleteChannel(computer);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
// Delete the computer shortcut itself
if (getInfoForId(computer.uuid) != null) {
sm.disableShortcuts(Collections.singletonList(computer.uuid), reason);
}
// Delete all associated app shortcuts too
List<ShortcutInfo> shortcuts = getAllShortcuts();
LinkedList<String> appShortcutIds = new LinkedList<>();
for (ShortcutInfo info : shortcuts) {
if (info.getId().startsWith(computer.uuid)) {
appShortcutIds.add(info.getId());
}
}
sm.disableShortcuts(appShortcutIds, reason);
}
}
public void disableShortcut(String uuid, CharSequence reason) {
tvChannelHelper.deleteChannel(uuid);
public void disableAppShortcut(ComputerDetails computer, NvApp app, CharSequence reason) {
tvChannelHelper.deleteProgram(computer, app);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
if (getInfoForId(uuid) != null) {
sm.disableShortcuts(Collections.singletonList(uuid), reason);
String id = getShortcutIdForGame(computer, app);
if (getInfoForId(id) != null) {
sm.disableShortcuts(Collections.singletonList(id), reason);
}
}
}

View File

@@ -21,6 +21,8 @@ import com.limelight.LimeLog;
import com.limelight.PosterContentProvider;
import com.limelight.R;
import com.limelight.ShortcutTrampoline;
import com.limelight.nvstream.http.ComputerDetails;
import com.limelight.nvstream.http.NvApp;
import java.io.IOException;
import java.io.OutputStream;
@@ -37,19 +39,19 @@ public class TvChannelHelper {
this.context = context;
}
void requestChannelOnHomeScreen(String computerUuid) {
void requestChannelOnHomeScreen(ComputerDetails computer) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!isAndroidTV()) {
return;
}
Long channelId = getChannelId(computerUuid);
Long channelId = getChannelId(computer.uuid);
if (channelId == null) {
return;
}
Intent intent = new Intent(TvContract.ACTION_REQUEST_CHANNEL_BROWSABLE);
intent.putExtra(TvContract.EXTRA_CHANNEL_ID, getChannelId(computerUuid));
intent.putExtra(TvContract.EXTRA_CHANNEL_ID, getChannelId(computer.uuid));
try {
context.startActivityForResult(intent, 0);
} catch (ActivityNotFoundException e) {
@@ -57,23 +59,23 @@ public class TvChannelHelper {
}
}
void createTvChannel(String computerUuid, String computerName) {
void createTvChannel(ComputerDetails computer) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!isAndroidTV()) {
return;
}
Intent i = new Intent(context, ShortcutTrampoline.class);
i.putExtra(AppView.NAME_EXTRA, computerName);
i.putExtra(AppView.UUID_EXTRA, computerUuid);
i.putExtra(AppView.NAME_EXTRA, computer.name);
i.putExtra(AppView.UUID_EXTRA, computer.uuid);
i.setAction(Intent.ACTION_DEFAULT);
ChannelBuilder builder = new ChannelBuilder()
.setType(TvContract.Channels.TYPE_PREVIEW)
.setDisplayName(computerName)
.setInternalProviderId(computerUuid)
.setDisplayName(computer.name)
.setInternalProviderId(computer.uuid)
.setAppLinkIntent(i);
Long channelId = getChannelId(computerUuid);
Long channelId = getChannelId(computer.uuid);
if (channelId != null) {
context.getContentResolver().update(TvContract.buildChannelUri(channelId),
builder.toContentValues(), null, null);
@@ -117,7 +119,7 @@ public class TvChannelHelper {
return bitmap;
}
void addGameToChannel(String computerUuid, String computerName, String appId, String appName) {
void addGameToChannel(ComputerDetails computer, NvApp app) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!isAndroidTV()) {
return;
@@ -126,29 +128,29 @@ public class TvChannelHelper {
PreviewProgramBuilder builder = new PreviewProgramBuilder();
Intent i = new Intent(context, ShortcutTrampoline.class);
i.putExtra(AppView.NAME_EXTRA, computerName);
i.putExtra(AppView.UUID_EXTRA, computerUuid);
i.putExtra(ShortcutTrampoline.APP_ID_EXTRA, appId);
i.putExtra(AppView.NAME_EXTRA, computer.name);
i.putExtra(AppView.UUID_EXTRA, computer.uuid);
i.putExtra(ShortcutTrampoline.APP_ID_EXTRA, ""+app.getAppId());
i.setAction(Intent.ACTION_DEFAULT);
Uri resourceURI = PosterContentProvider.createBoxArtUri(computerUuid, appId);
Uri resourceURI = PosterContentProvider.createBoxArtUri(computer.uuid, ""+app.getAppId());
Long channelId = getChannelId(computerUuid);
Long channelId = getChannelId(computer.uuid);
if (channelId == null) {
return;
}
builder.setChannelId(channelId)
.setType(TYPE_GAME)
.setTitle(appName)
.setTitle(app.getAppName())
.setPosterArtAspectRatio(ASPECT_RATIO_MOVIE_POSTER)
.setPosterArtUri(resourceURI)
.setIntent(i)
.setInternalProviderId(appId)
.setInternalProviderId(""+app.getAppId())
// Weight should increase each time we run the game
.setWeight((int)((System.currentTimeMillis() - 1500000000000L) / 1000));
Long programId = getProgramId(channelId, appId);
Long programId = getProgramId(channelId, ""+app.getAppId());
if (programId != null) {
context.getContentResolver().update(TvContract.buildPreviewProgramUri(programId),
builder.toContentValues(), null, null);
@@ -162,18 +164,39 @@ public class TvChannelHelper {
}
}
void deleteChannel(String computerUuid) {
void deleteChannel(ComputerDetails computer) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!isAndroidTV()) {
return;
}
Long channelId = getChannelId(computerUuid);
Long channelId = getChannelId(computer.uuid);
if (channelId == null) {
return;
}
Uri uri = TvContract.buildChannelUri(channelId);
context.getContentResolver().delete(uri, null, null);
context.getContentResolver().delete(TvContract.buildChannelUri(channelId), null, null);
}
}
void deleteProgram(ComputerDetails computer, NvApp app) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (!isAndroidTV()) {
return;
}
Long channelId = getChannelId(computer.uuid);
if (channelId == null) {
return;
}
Long programId = getProgramId(channelId, ""+app.getAppId());
if (programId == null) {
return;
}
context.getContentResolver().delete(TvContract.buildPreviewProgramUri(programId), null, null);
}
}