Add capability-based TV input button and switching
This commit is contained in:
parent
22a2130563
commit
5f035d909a
13 changed files with 374 additions and 11 deletions
|
|
@ -6,8 +6,8 @@ android {
|
|||
applicationId 'tv.luma.launcher'
|
||||
minSdk 26
|
||||
targetSdk 34
|
||||
versionCode 12
|
||||
versionName '1.1.9'
|
||||
versionCode 13
|
||||
versionName '1.1.10'
|
||||
}
|
||||
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8; targetCompatibility JavaVersion.VERSION_1_8 }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tv.luma.launcher" android:versionCode="12" android:versionName="1.1.9">
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tv.luma.launcher" android:versionCode="13" android:versionName="1.1.10">
|
||||
<uses-sdk android:minSdkVersion="26" android:targetSdkVersion="34" />
|
||||
<uses-feature android:name="android.software.leanback" android:required="true" />
|
||||
<uses-feature android:name="android.hardware.touchscreen" android:required="false" />
|
||||
|
|
@ -8,10 +8,12 @@
|
|||
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
<queries>
|
||||
<intent><action android:name="android.intent.action.VIEW" /><data android:scheme="content" android:host="android.media.tv" android:mimeType="vnd.android.cursor.item/channel" /></intent>
|
||||
<intent><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LEANBACK_LAUNCHER" /></intent>
|
||||
<intent><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent>
|
||||
</queries>
|
||||
<application android:label="Luma" android:icon="@drawable/ic_luma" android:banner="@drawable/banner" android:theme="@style/AppTheme" android:allowBackup="false" android:supportsRtl="true" android:usesCleartextTraffic="true" android:hardwareAccelerated="true" android:largeHeap="false">
|
||||
<activity android:name=".InputActivity" android:exported="false" android:screenOrientation="landscape" android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize" />
|
||||
<activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTask" android:screenOrientation="landscape" android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize" android:excludeFromRecents="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
|
|
|||
117
app/src/main/java/tv/luma/launcher/InputActivity.java
Normal file
117
app/src/main/java/tv/luma/launcher/InputActivity.java
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package tv.luma.launcher;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.media.*;
|
||||
import android.media.tv.*;
|
||||
import android.os.Bundle;
|
||||
import android.view.*;
|
||||
import android.widget.*;
|
||||
|
||||
/** Standard TV-input viewer for devices without an available native channel-view activity. */
|
||||
public final class InputActivity extends Activity {
|
||||
private TvView tv;
|
||||
private TextView status;
|
||||
private TvInputs.Source source;
|
||||
private AudioManager audio;
|
||||
private AudioFocusRequest focus;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
String id = getIntent().getStringExtra("inputId");
|
||||
for (TvInputs.Source item : new TvInputs(this).list()) if (item.id.equals(id)) source = item;
|
||||
if (source == null) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
setTitle(source.label);
|
||||
getWindow().getDecorView().setSystemUiVisibility(5894);
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
FrameLayout root = new FrameLayout(this);
|
||||
root.setBackgroundColor(Color.BLACK);
|
||||
tv = new TvView(this);
|
||||
tv.setFocusable(true);
|
||||
tv.setFocusableInTouchMode(true);
|
||||
root.addView(tv, new FrameLayout.LayoutParams(-1, -1));
|
||||
status = Ui.text(this, "Switching to " + source.label + "…", 20, Ui.WHITE);
|
||||
status.setGravity(Gravity.CENTER);
|
||||
root.addView(status, new FrameLayout.LayoutParams(-1, -1));
|
||||
setContentView(root);
|
||||
tv.requestFocus();
|
||||
tv.setCallback(
|
||||
new TvView.TvInputCallback() {
|
||||
@Override
|
||||
public void onConnectionFailed(String id) {
|
||||
showStatus("Input unavailable · Press Back to return");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnected(String id) {
|
||||
showStatus("Input unavailable · Press Back to return");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onVideoAvailable(String id) {
|
||||
status.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onVideoUnavailable(String id, int reason) {
|
||||
if (reason != TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING
|
||||
&& reason != TvInputManager.VIDEO_UNAVAILABLE_REASON_BUFFERING)
|
||||
showStatus("No signal · Press Back to return");
|
||||
}
|
||||
});
|
||||
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
|
||||
focus =
|
||||
new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
|
||||
.setAudioAttributes(
|
||||
new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_MEDIA)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
|
||||
.build())
|
||||
.setOnAudioFocusChangeListener(
|
||||
change -> {
|
||||
if (tv != null)
|
||||
tv.setStreamVolume(change == AudioManager.AUDIOFOCUS_GAIN ? 1 : 0);
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
private void showStatus(String message) {
|
||||
status.setText(source.label + "\n" + message);
|
||||
status.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (tv == null) return;
|
||||
try {
|
||||
showStatus("Switching input…");
|
||||
int granted = audio.requestAudioFocus(focus);
|
||||
tv.setStreamVolume(granted == AudioManager.AUDIOFOCUS_REQUEST_GRANTED ? 1 : 0);
|
||||
tv.tune(source.id, TvContract.buildChannelUriForPassthroughInput(source.id));
|
||||
} catch (RuntimeException e) {
|
||||
showStatus("Input unavailable · Press Back to return");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
if (tv != null) tv.reset();
|
||||
if (audio != null && focus != null) audio.abandonAudioFocusRequest(focus);
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean dispatchKeyEvent(KeyEvent event) {
|
||||
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
|
||||
if (event.getAction() == KeyEvent.ACTION_UP) finish();
|
||||
return true;
|
||||
}
|
||||
return super.dispatchKeyEvent(event);
|
||||
}
|
||||
}
|
||||
|
|
@ -25,6 +25,11 @@ public final class MainActivity extends Activity {
|
|||
private LinearLayout content, appsContainer, mediaContainer, hero, sectionHeading;
|
||||
private PlexClient.Media plexFeatured;
|
||||
private TextView clock, eyebrow, headline, description, openHint, appCount, settingsButton;
|
||||
private ImageButton inputButton;
|
||||
private TvInputs tvInputs;
|
||||
private List<TvInputs.Source> inputSources = Collections.emptyList();
|
||||
private volatile int inputGeneration;
|
||||
private final Runnable refreshInputTask = this::refreshInputs;
|
||||
private final List<AppRepository.App> allApps = new ArrayList<>(),
|
||||
visibleApps = new ArrayList<>();
|
||||
private final Map<String, AppTile> tiles = new LinkedHashMap<>();
|
||||
|
|
@ -80,6 +85,7 @@ public final class MainActivity extends Activity {
|
|||
network.allowCoreThreadTimeOut(true);
|
||||
prefs = getSharedPreferences("luma", MODE_PRIVATE);
|
||||
repository = new AppRepository(this);
|
||||
tvInputs = new TvInputs(this);
|
||||
plex = new PlexClient(this);
|
||||
saverMedia = new SaverMedia(this);
|
||||
updater = new Updater(this);
|
||||
|
|
@ -161,10 +167,24 @@ public final class MainActivity extends Activity {
|
|||
clock = Ui.text(this, "", 13, Ui.WHITE);
|
||||
clock.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
|
||||
header.addView(clock, new LinearLayout.LayoutParams(d(125), d(32)));
|
||||
inputButton = new ImageButton(this);
|
||||
inputButton.setImageResource(R.drawable.ic_input);
|
||||
inputButton.setImageTintList(Ui.textColors());
|
||||
inputButton.setPadding(d(9), d(7), d(9), d(7));
|
||||
inputButton.setFocusable(true);
|
||||
inputButton.setContentDescription("TV inputs");
|
||||
inputButton.setId(View.generateViewId());
|
||||
Ui.glass(inputButton, 18);
|
||||
inputButton.setVisibility(View.GONE);
|
||||
inputButton.setOnClickListener(v -> showInputs());
|
||||
LinearLayout.LayoutParams inputLp = new LinearLayout.LayoutParams(d(39), d(34));
|
||||
inputLp.leftMargin = d(17);
|
||||
header.addView(inputButton, inputLp);
|
||||
settingsButton = chip("⚙", v -> showSettings());
|
||||
settingsButton.setTextSize(19);
|
||||
settingsButton.setContentDescription("Luma settings");
|
||||
settingsButton.setId(View.generateViewId());
|
||||
inputButton.setNextFocusRightId(settingsButton.getId());
|
||||
LinearLayout.LayoutParams settingsLp = new LinearLayout.LayoutParams(d(39), d(34));
|
||||
settingsLp.leftMargin = d(17);
|
||||
header.addView(settingsButton, settingsLp);
|
||||
|
|
@ -308,6 +328,13 @@ public final class MainActivity extends Activity {
|
|||
protected void onResume() {
|
||||
super.onResume();
|
||||
foreground = true;
|
||||
tvInputs.start(
|
||||
() -> {
|
||||
handler.removeCallbacks(refreshInputTask);
|
||||
handler.postDelayed(refreshInputTask, 100);
|
||||
},
|
||||
handler);
|
||||
refreshInputs();
|
||||
scene.resume();
|
||||
plex.resume();
|
||||
restoreImages();
|
||||
|
|
@ -337,6 +364,9 @@ public final class MainActivity extends Activity {
|
|||
protected void onPause() {
|
||||
super.onPause();
|
||||
foreground = false;
|
||||
tvInputs.stop();
|
||||
handler.removeCallbacks(refreshInputTask);
|
||||
inputGeneration++;
|
||||
generation++;
|
||||
artGeneration++;
|
||||
work.getQueue().clear();
|
||||
|
|
@ -544,8 +574,10 @@ public final class MainActivity extends Activity {
|
|||
appsContainer.addView(empty, new LinearLayout.LayoutParams(-1, d(70)));
|
||||
}
|
||||
appCount.setText(visibleApps.size() + " installed");
|
||||
if (!tiles.isEmpty())
|
||||
if (!tiles.isEmpty()) {
|
||||
settingsButton.setNextFocusDownId(tiles.values().iterator().next().getId());
|
||||
inputButton.setNextFocusDownId(tiles.values().iterator().next().getId());
|
||||
}
|
||||
String restore =
|
||||
tiles.containsKey(selected)
|
||||
? selected
|
||||
|
|
@ -841,6 +873,51 @@ public final class MainActivity extends Activity {
|
|||
updateButton.setVisibility(updater.available() == null ? View.GONE : View.VISIBLE);
|
||||
}
|
||||
|
||||
private void refreshInputs() {
|
||||
if (!foreground) return;
|
||||
final int ticket = ++inputGeneration;
|
||||
work.execute(
|
||||
() -> {
|
||||
if (!foreground || ticket != inputGeneration) return;
|
||||
List<TvInputs.Source> sources = tvInputs.list();
|
||||
handler.post(
|
||||
() -> {
|
||||
if (!foreground || ticket != inputGeneration) return;
|
||||
inputSources = sources;
|
||||
boolean available = !sources.isEmpty();
|
||||
if (!available && inputButton.hasFocus()) settingsButton.requestFocus();
|
||||
inputButton.setVisibility(available ? View.VISIBLE : View.GONE);
|
||||
settingsButton.setNextFocusLeftId(available ? inputButton.getId() : View.NO_ID);
|
||||
if (!tiles.isEmpty())
|
||||
inputButton.setNextFocusDownId(tiles.values().iterator().next().getId());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void showInputs() {
|
||||
if (inputSources.isEmpty()) return;
|
||||
LinearLayout p = panel("TV inputs", "Choose an input");
|
||||
for (TvInputs.Source source : inputSources)
|
||||
action(
|
||||
p,
|
||||
source.label,
|
||||
source.state,
|
||||
() -> {
|
||||
dismiss();
|
||||
Intent nativeViewer = tvInputs.nativeIntent(source);
|
||||
if (nativeViewer != null) {
|
||||
try {
|
||||
startActivity(nativeViewer);
|
||||
return;
|
||||
} catch (RuntimeException e) {
|
||||
/* Try TvView. */
|
||||
}
|
||||
}
|
||||
startActivity(new Intent(this, InputActivity.class).putExtra("inputId", source.id));
|
||||
});
|
||||
action(p, "Cancel", null, this::dismiss);
|
||||
}
|
||||
|
||||
private void updatesSettings() {
|
||||
Updater.Release release = updater.available();
|
||||
long checked = updater.prefs.getLong("success", 0);
|
||||
|
|
|
|||
137
app/src/main/java/tv/luma/launcher/TvInputs.java
Normal file
137
app/src/main/java/tv/luma/launcher/TvInputs.java
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
package tv.luma.launcher;
|
||||
|
||||
import android.content.*;
|
||||
import android.content.pm.*;
|
||||
import android.media.tv.*;
|
||||
import android.os.Handler;
|
||||
import java.util.*;
|
||||
|
||||
/** Physical input capabilities supplied by Android, independent of the device vendor. */
|
||||
final class TvInputs {
|
||||
static final class Source {
|
||||
final String id, label, state;
|
||||
|
||||
Source(String id, String label, String state) {
|
||||
this.id = id;
|
||||
this.label = label;
|
||||
this.state = state;
|
||||
}
|
||||
}
|
||||
|
||||
private final Context context;
|
||||
private final TvInputManager manager;
|
||||
private TvInputManager.TvInputCallback callback;
|
||||
|
||||
TvInputs(Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
manager = (TvInputManager) this.context.getSystemService(Context.TV_INPUT_SERVICE);
|
||||
}
|
||||
|
||||
List<Source> list() {
|
||||
List<Source> result = new ArrayList<>();
|
||||
if (manager == null) return result;
|
||||
try {
|
||||
List<TvInputInfo> inputs = manager.getTvInputList();
|
||||
Set<String> parents = new HashSet<>();
|
||||
for (TvInputInfo input : inputs)
|
||||
if (physical(input) && !input.isHidden(context)) parents.add(input.getId());
|
||||
for (TvInputInfo input : inputs) {
|
||||
if (!parents.contains(input.getId()) || parents.contains(input.getParentId())) continue;
|
||||
CharSequence label = input.loadCustomLabel(context);
|
||||
if (label == null || label.length() == 0) label = input.loadLabel(context);
|
||||
if (label == null || label.length() == 0) continue;
|
||||
int state = manager.getInputState(input.getId());
|
||||
result.add(
|
||||
new Source(
|
||||
input.getId(),
|
||||
label.toString(),
|
||||
state == TvInputManager.INPUT_STATE_DISCONNECTED
|
||||
? "No signal"
|
||||
: state == TvInputManager.INPUT_STATE_CONNECTED_STANDBY
|
||||
? "Standby"
|
||||
: "Connected"));
|
||||
}
|
||||
result.sort((a, b) -> a.label.compareToIgnoreCase(b.label));
|
||||
} catch (RuntimeException e) {
|
||||
// Some Android TV implementations do not expose inputs to third-party apps.
|
||||
result.clear();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean physical(TvInputInfo input) {
|
||||
return input.isPassthroughInput()
|
||||
&& input.getType() != TvInputInfo.TYPE_OTHER
|
||||
&& input.getType() != TvInputInfo.TYPE_TUNER;
|
||||
}
|
||||
|
||||
Intent nativeIntent(Source source) {
|
||||
Intent intent =
|
||||
new Intent(Intent.ACTION_VIEW)
|
||||
.setDataAndType(
|
||||
TvContract.buildChannelUriForPassthroughInput(source.id),
|
||||
TvContract.Channels.CONTENT_ITEM_TYPE);
|
||||
PackageManager pm = context.getPackageManager();
|
||||
try {
|
||||
for (ResolveInfo match :
|
||||
pm.queryIntentActivities(
|
||||
intent, PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_RESOLVED_FILTER)) {
|
||||
ActivityInfo activity = match.activityInfo;
|
||||
// Exclude generic content browsers; only use an actual TV channel viewer.
|
||||
if (match.filter == null
|
||||
|| !match.filter.hasDataType(TvContract.Channels.CONTENT_ITEM_TYPE)
|
||||
|| !activity.exported
|
||||
|| !activity.enabled) continue;
|
||||
if (activity.permission != null
|
||||
&& pm.checkPermission(activity.permission, context.getPackageName())
|
||||
!= PackageManager.PERMISSION_GRANTED) continue;
|
||||
return intent.setComponent(new ComponentName(activity.packageName, activity.name));
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
/* Use the platform TvView fallback. */
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void start(Runnable changed, Handler handler) {
|
||||
if (manager == null || callback != null) return;
|
||||
TvInputManager.TvInputCallback listener =
|
||||
new TvInputManager.TvInputCallback() {
|
||||
@Override
|
||||
public void onInputAdded(String id) {
|
||||
changed.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInputRemoved(String id) {
|
||||
changed.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInputUpdated(String id) {
|
||||
changed.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInputStateChanged(String id, int state) {
|
||||
changed.run();
|
||||
}
|
||||
};
|
||||
try {
|
||||
manager.registerCallback(listener, handler);
|
||||
callback = listener;
|
||||
} catch (RuntimeException e) {
|
||||
/* Initial discovery can still be used. */
|
||||
}
|
||||
}
|
||||
|
||||
void stop() {
|
||||
if (callback == null) return;
|
||||
try {
|
||||
manager.unregisterCallback(callback);
|
||||
} catch (RuntimeException e) {
|
||||
/* Already gone. */
|
||||
}
|
||||
callback = null;
|
||||
}
|
||||
}
|
||||
4
app/src/main/res/drawable/ic_input.xml
Normal file
4
app/src/main/res/drawable/ic_input.xml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24">
|
||||
<path android:fillColor="#FFFFFFFF" android:pathData="M19,3H5C3.9,3 3,3.9 3,5V8H5V5H19V19H5V16H3V19C3,20.1 3.9,21 5,21H19C20.1,21 21,20.1 21,19V5C21,3.9 20.1,3 19,3ZM11,7L9.6,8.4L12.2,11H2V13H12.2L9.6,15.6L11,17L16,12Z" />
|
||||
</vector>
|
||||
Loading…
Add table
Add a link
Reference in a new issue