2015-01-14 12:12:47 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Synced Synapse. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2015-03-09 22:35:18 +01:00
|
|
|
package org.xbmc.kore.ui;
|
2015-01-14 12:12:47 +01:00
|
|
|
|
2016-02-05 16:03:05 +01:00
|
|
|
import android.content.Context;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.content.Intent;
|
2015-06-21 16:04:54 +02:00
|
|
|
import android.content.SharedPreferences;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.graphics.Point;
|
2015-04-08 23:28:19 +02:00
|
|
|
import android.net.Uri;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.os.Bundle;
|
2015-01-23 23:23:59 +01:00
|
|
|
import android.os.Handler;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2016-01-25 00:55:34 +01:00
|
|
|
import android.support.v4.text.TextDirectionHeuristicsCompat;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.support.v4.view.ViewPager;
|
|
|
|
import android.support.v4.widget.DrawerLayout;
|
2015-06-23 20:08:45 +02:00
|
|
|
import android.support.v7.app.ActionBar;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2016-02-05 16:03:05 +01:00
|
|
|
import android.telephony.PhoneStateListener;
|
|
|
|
import android.telephony.TelephonyManager;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.text.TextUtils;
|
2015-02-14 06:26:19 +01:00
|
|
|
import android.view.KeyEvent;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.ViewTreeObserver;
|
2016-02-19 22:20:18 +01:00
|
|
|
import android.view.WindowManager;
|
2015-01-14 12:12:47 +01:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2015-03-09 22:35:18 +01:00
|
|
|
import org.xbmc.kore.R;
|
|
|
|
import org.xbmc.kore.Settings;
|
|
|
|
import org.xbmc.kore.host.HostConnectionObserver;
|
|
|
|
import org.xbmc.kore.host.HostManager;
|
|
|
|
import org.xbmc.kore.jsonrpc.ApiCallback;
|
2015-04-10 20:51:44 +02:00
|
|
|
import org.xbmc.kore.jsonrpc.HostConnection;
|
2015-03-09 22:35:18 +01:00
|
|
|
import org.xbmc.kore.jsonrpc.method.Application;
|
|
|
|
import org.xbmc.kore.jsonrpc.method.AudioLibrary;
|
2015-03-20 00:00:31 +01:00
|
|
|
import org.xbmc.kore.jsonrpc.method.GUI;
|
2015-03-09 22:35:18 +01:00
|
|
|
import org.xbmc.kore.jsonrpc.method.Input;
|
2015-04-08 23:28:19 +02:00
|
|
|
import org.xbmc.kore.jsonrpc.method.Player;
|
|
|
|
import org.xbmc.kore.jsonrpc.method.Playlist;
|
2015-03-09 22:35:18 +01:00
|
|
|
import org.xbmc.kore.jsonrpc.method.System;
|
|
|
|
import org.xbmc.kore.jsonrpc.method.VideoLibrary;
|
|
|
|
import org.xbmc.kore.jsonrpc.type.GlobalType;
|
|
|
|
import org.xbmc.kore.jsonrpc.type.ListType;
|
|
|
|
import org.xbmc.kore.jsonrpc.type.PlayerType;
|
2015-04-08 23:28:19 +02:00
|
|
|
import org.xbmc.kore.jsonrpc.type.PlaylistType;
|
2016-05-20 20:24:34 +02:00
|
|
|
import org.xbmc.kore.service.ConnectionObserversManagerService;
|
2015-03-09 22:35:18 +01:00
|
|
|
import org.xbmc.kore.ui.hosts.AddHostActivity;
|
2015-12-15 20:08:19 +01:00
|
|
|
import org.xbmc.kore.ui.hosts.AddHostFragmentFinish;
|
2015-03-09 22:35:18 +01:00
|
|
|
import org.xbmc.kore.ui.views.CirclePageIndicator;
|
|
|
|
import org.xbmc.kore.utils.LogUtils;
|
|
|
|
import org.xbmc.kore.utils.TabsAdapter;
|
|
|
|
import org.xbmc.kore.utils.UIUtils;
|
2015-04-08 23:28:19 +02:00
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2015-01-14 12:12:47 +01:00
|
|
|
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
import butterknife.InjectView;
|
|
|
|
|
|
|
|
|
2015-02-11 00:17:16 +01:00
|
|
|
public class RemoteActivity extends BaseActivity
|
2015-01-14 12:12:47 +01:00
|
|
|
implements HostConnectionObserver.PlayerEventsObserver,
|
|
|
|
NowPlayingFragment.NowPlayingListener,
|
|
|
|
SendTextDialogFragment.SendTextDialogListener {
|
|
|
|
private static final String TAG = LogUtils.makeLogTag(RemoteActivity.class);
|
|
|
|
|
2016-03-24 15:26:34 +01:00
|
|
|
|
|
|
|
private static final int NOWPLAYING_FRAGMENT_ID = 1;
|
|
|
|
private static final int REMOTE_FRAGMENT_ID = 2;
|
|
|
|
private static final int PLAYLIST_FRAGMENT_ID = 3;
|
|
|
|
|
2015-01-14 12:12:47 +01:00
|
|
|
/**
|
|
|
|
* Host manager singleton
|
|
|
|
*/
|
|
|
|
private HostManager hostManager = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* To register for observing host events
|
|
|
|
*/
|
|
|
|
private HostConnectionObserver hostConnectionObserver;
|
|
|
|
|
|
|
|
private NavigationDrawerFragment navigationDrawerFragment;
|
|
|
|
|
|
|
|
@InjectView(R.id.background_image) ImageView backgroundImage;
|
|
|
|
@InjectView(R.id.pager_indicator) CirclePageIndicator pageIndicator;
|
|
|
|
@InjectView(R.id.pager) ViewPager viewPager;
|
|
|
|
@InjectView(R.id.default_toolbar) Toolbar toolbar;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
// Set default values for the preferences
|
|
|
|
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
|
|
|
|
|
|
|
|
setContentView(R.layout.activity_remote);
|
|
|
|
ButterKnife.inject(this);
|
|
|
|
|
|
|
|
hostManager = HostManager.getInstance(this);
|
|
|
|
|
|
|
|
// Check if we have any hosts setup
|
|
|
|
if (hostManager.getHostInfo() == null) {
|
|
|
|
final Intent intent = new Intent(this, AddHostActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
2016-11-16 13:10:20 +01:00
|
|
|
return;
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the drawer.
|
|
|
|
navigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
|
|
|
|
.findFragmentById(R.id.navigation_drawer);
|
|
|
|
navigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
|
|
|
|
|
|
|
|
// Set up pager and fragments
|
|
|
|
TabsAdapter tabsAdapter = new TabsAdapter(this, getSupportFragmentManager())
|
2016-03-24 15:26:34 +01:00
|
|
|
.addTab(NowPlayingFragment.class, null, R.string.now_playing, NOWPLAYING_FRAGMENT_ID)
|
|
|
|
.addTab(RemoteFragment.class, null, R.string.remote, REMOTE_FRAGMENT_ID)
|
|
|
|
.addTab(PlaylistFragment.class, null, R.string.playlist, PLAYLIST_FRAGMENT_ID);
|
2015-01-14 12:12:47 +01:00
|
|
|
|
|
|
|
viewPager.setAdapter(tabsAdapter);
|
|
|
|
pageIndicator.setViewPager(viewPager);
|
|
|
|
pageIndicator.setOnPageChangeListener(defaultOnPageChangeListener);
|
|
|
|
|
|
|
|
viewPager.setCurrentItem(1);
|
|
|
|
viewPager.setOffscreenPageLimit(2);
|
|
|
|
|
|
|
|
setupActionBar();
|
|
|
|
|
2016-11-15 20:09:23 +01:00
|
|
|
// Periodic Check of Kodi version
|
|
|
|
hostManager.checkAndUpdateKodiVersion(hostManager.getHostInfo());
|
|
|
|
|
2015-04-08 23:28:19 +02:00
|
|
|
// If we should start playing something
|
|
|
|
|
2015-01-14 12:12:47 +01:00
|
|
|
// // Setup system bars and content padding
|
|
|
|
// setupSystemBarsColors();
|
|
|
|
// // Set the padding of views.
|
|
|
|
// // Only set top and right, to allow bottom to overlap in each fragment
|
|
|
|
// UIUtils.setPaddingForSystemBars(this, viewPager, true, true, false);
|
|
|
|
// UIUtils.setPaddingForSystemBars(this, pageIndicator, true, true, false);
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:08:05 +02:00
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
handleStartIntent(getIntent());
|
|
|
|
}
|
|
|
|
|
2015-01-14 12:12:47 +01:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
hostConnectionObserver = hostManager.getHostConnectionObserver();
|
2015-02-15 20:11:32 +01:00
|
|
|
hostConnectionObserver.registerPlayerObserver(this, true);
|
|
|
|
// Force a refresh, mainly to update the time elapsed on the fragments
|
|
|
|
hostConnectionObserver.forceRefreshResults();
|
2015-12-15 20:08:19 +01:00
|
|
|
|
2016-02-19 22:20:18 +01:00
|
|
|
// Check whether we should keep the remote activity above the lockscreen
|
|
|
|
boolean keepAboveLockscreen = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(this)
|
|
|
|
.getBoolean(Settings.KEY_PREF_KEEP_REMOTE_ABOVE_LOCKSCREEN,
|
|
|
|
Settings.DEFAULT_KEY_PREF_KEEP_REMOTE_ABOVE_LOCKSCREEN);
|
|
|
|
if (keepAboveLockscreen) {
|
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
|
|
|
|
} else {
|
|
|
|
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
|
|
|
|
}
|
|
|
|
|
2016-09-07 20:33:58 +02:00
|
|
|
// Check whether we should keep the screen on
|
|
|
|
boolean keepScreenOn = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(this)
|
|
|
|
.getBoolean(Settings.KEY_PREF_KEEP_SCREEN_ON,
|
|
|
|
Settings.DEFAULT_KEY_PREF_KEEP_SCREEN_ON);
|
|
|
|
if (keepScreenOn) {
|
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
|
|
} else {
|
|
|
|
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
|
|
|
}
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
2015-12-02 20:39:18 +01:00
|
|
|
if (hostConnectionObserver != null) hostConnectionObserver.unregisterPlayerObserver(this);
|
2015-01-14 12:12:47 +01:00
|
|
|
hostConnectionObserver = null;
|
|
|
|
}
|
|
|
|
|
2015-02-14 06:26:19 +01:00
|
|
|
/**
|
|
|
|
* Override hardware volume keys and send to Kodi
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean dispatchKeyEvent(KeyEvent event) {
|
2015-02-15 20:34:56 +01:00
|
|
|
// Check whether we should intercept this
|
|
|
|
boolean useVolumeKeys = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(this)
|
|
|
|
.getBoolean(Settings.KEY_PREF_USE_HARDWARE_VOLUME_KEYS,
|
|
|
|
Settings.DEFAULT_PREF_USE_HARDWARE_VOLUME_KEYS);
|
|
|
|
if (useVolumeKeys) {
|
|
|
|
int action = event.getAction();
|
|
|
|
int keyCode = event.getKeyCode();
|
|
|
|
switch (keyCode) {
|
|
|
|
case KeyEvent.KEYCODE_VOLUME_UP:
|
|
|
|
if (action == KeyEvent.ACTION_DOWN) {
|
2015-04-08 23:28:19 +02:00
|
|
|
new Application
|
|
|
|
.SetVolume(GlobalType.IncrementDecrement.INCREMENT)
|
|
|
|
.execute(hostManager.getConnection(), null, null);
|
2015-02-15 20:34:56 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case KeyEvent.KEYCODE_VOLUME_DOWN:
|
|
|
|
if (action == KeyEvent.ACTION_DOWN) {
|
2015-04-08 23:28:19 +02:00
|
|
|
new Application
|
|
|
|
.SetVolume(GlobalType.IncrementDecrement.DECREMENT)
|
|
|
|
.execute(hostManager.getConnection(), null, null);
|
2015-02-15 20:34:56 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-14 06:26:19 +01:00
|
|
|
}
|
2015-02-15 20:34:56 +01:00
|
|
|
|
|
|
|
return super.dispatchKeyEvent(event);
|
2015-02-14 06:26:19 +01:00
|
|
|
}
|
|
|
|
|
2015-01-14 12:12:47 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
if (!navigationDrawerFragment.isDrawerOpen()) {
|
|
|
|
// Only show items in the action bar relevant to this screen if the drawer is not showing.
|
|
|
|
// Otherwise, let the drawer decide what to show in the action bar.
|
|
|
|
getMenuInflater().inflate(R.menu.remote, menu);
|
|
|
|
}
|
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
// Handle action bar item clicks here.
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_wake_up:
|
|
|
|
UIUtils.sendWolAsync(this, hostManager.getHostInfo());
|
|
|
|
return true;
|
|
|
|
case R.id.action_quit:
|
|
|
|
Application.Quit actionQuit = new Application.Quit();
|
|
|
|
// Fire and forget
|
|
|
|
actionQuit.execute(hostManager.getConnection(), null, null);
|
|
|
|
return true;
|
|
|
|
case R.id.action_suspend:
|
|
|
|
System.Suspend actionSuspend = new System.Suspend();
|
|
|
|
// Fire and forget
|
|
|
|
actionSuspend.execute(hostManager.getConnection(), null, null);
|
|
|
|
return true;
|
2015-03-29 13:48:51 +02:00
|
|
|
case R.id.action_reboot:
|
|
|
|
System.Reboot actionReboot = new System.Reboot();
|
|
|
|
// Fire and forget
|
|
|
|
actionReboot.execute(hostManager.getConnection(), null, null);
|
|
|
|
return true;
|
2015-01-14 12:12:47 +01:00
|
|
|
case R.id.action_shutdown:
|
|
|
|
System.Shutdown actionShutdown = new System.Shutdown();
|
|
|
|
// Fire and forget
|
|
|
|
actionShutdown.execute(hostManager.getConnection(), null, null);
|
|
|
|
return true;
|
|
|
|
case R.id.send_text:
|
|
|
|
SendTextDialogFragment dialog =
|
|
|
|
SendTextDialogFragment.newInstance(getString(R.string.send_text));
|
|
|
|
dialog.show(getSupportFragmentManager(), null);
|
2015-01-23 23:23:59 +01:00
|
|
|
return true;
|
2015-01-25 15:17:22 +01:00
|
|
|
case R.id.toggle_fullscreen:
|
2015-03-20 00:00:31 +01:00
|
|
|
GUI.SetFullscreen actionSetFullscreen = new GUI.SetFullscreen();
|
|
|
|
// Input.ExecuteAction actionSetFullscreen = new Input.ExecuteAction(Input.ExecuteAction.TOGGLEFULLSCREEN);
|
2015-01-25 15:17:22 +01:00
|
|
|
actionSetFullscreen.execute(hostManager.getConnection(), null, null);
|
|
|
|
return true;
|
2015-01-23 23:23:59 +01:00
|
|
|
case R.id.clean_video_library:
|
|
|
|
VideoLibrary.Clean actionCleanVideo = new VideoLibrary.Clean();
|
|
|
|
actionCleanVideo.execute(hostManager.getConnection(), null, null);
|
|
|
|
return true;
|
|
|
|
case R.id.clean_audio_library:
|
|
|
|
AudioLibrary.Clean actionCleanAudio = new AudioLibrary.Clean();
|
|
|
|
actionCleanAudio.execute(hostManager.getConnection(), null, null);
|
|
|
|
return true;
|
|
|
|
case R.id.update_video_library:
|
|
|
|
VideoLibrary.Scan actionScanVideo = new VideoLibrary.Scan();
|
|
|
|
actionScanVideo.execute(hostManager.getConnection(), null, null);
|
|
|
|
return true;
|
|
|
|
case R.id.update_audio_library:
|
|
|
|
AudioLibrary.Scan actionScanAudio = new AudioLibrary.Scan();
|
|
|
|
actionScanAudio.execute(hostManager.getConnection(), null, null);
|
2015-01-14 12:12:47 +01:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callbacks from Send text dialog
|
|
|
|
*/
|
|
|
|
public void onSendTextFinished(String text, boolean done) {
|
2016-01-25 00:55:34 +01:00
|
|
|
if (TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR.isRtl(text, 0, text.length())) {
|
|
|
|
text = new StringBuilder(text).reverse().toString();
|
|
|
|
}
|
2015-01-14 12:12:47 +01:00
|
|
|
Input.SendText action = new Input.SendText(text, done);
|
|
|
|
action.execute(hostManager.getConnection(), null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onSendTextCancel() {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void setupActionBar() {
|
2016-03-24 15:26:34 +01:00
|
|
|
setToolbarTitle(toolbar, NOWPLAYING_FRAGMENT_ID);
|
2015-01-14 12:12:47 +01:00
|
|
|
setSupportActionBar(toolbar);
|
2015-06-23 20:08:45 +02:00
|
|
|
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
if (actionBar == null) return;
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setToolbarTitle(Toolbar toolbar, int position) {
|
|
|
|
if (toolbar != null) {
|
|
|
|
switch (position) {
|
|
|
|
case 0:
|
|
|
|
toolbar.setTitle(R.string.now_playing);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
toolbar.setTitle(R.string.remote);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
toolbar.setTitle(R.string.playlist);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 23:28:19 +02:00
|
|
|
/**
|
|
|
|
* Handles the intent that started this activity, namely to start playing something on Kodi
|
|
|
|
* @param intent Start intent for the activity
|
|
|
|
*/
|
|
|
|
private void handleStartIntent(Intent intent) {
|
|
|
|
final String action = intent.getAction();
|
|
|
|
// Check action
|
2015-04-18 19:23:31 +02:00
|
|
|
if ((action == null) ||
|
|
|
|
!(action.equals(Intent.ACTION_SEND) || action.equals(Intent.ACTION_VIEW)))
|
|
|
|
return;
|
|
|
|
|
2016-01-08 17:54:59 +01:00
|
|
|
Uri videoUri = null;
|
2015-04-18 19:23:31 +02:00
|
|
|
if (action.equals(Intent.ACTION_SEND)) {
|
|
|
|
// Get the URI, which is stored in Extras
|
2016-01-08 17:54:59 +01:00
|
|
|
videoUri = getYouTubeUri(intent.getStringExtra(Intent.EXTRA_TEXT));
|
|
|
|
if (videoUri == null) return;
|
2015-04-18 19:23:31 +02:00
|
|
|
} else if (action.equals(Intent.ACTION_VIEW)) {
|
|
|
|
if (intent.getData() == null) return;
|
2016-01-08 17:54:59 +01:00
|
|
|
videoUri = Uri.parse(intent.getData().toString());
|
2015-04-18 19:23:31 +02:00
|
|
|
}
|
2015-04-08 23:28:19 +02:00
|
|
|
|
2016-01-08 17:54:59 +01:00
|
|
|
final String videoId = getVideoId(videoUri);
|
2016-10-18 20:06:41 +02:00
|
|
|
String videoUrl;
|
|
|
|
if (videoId != null) {
|
|
|
|
videoUrl = "plugin://plugin.video." +
|
2016-01-08 17:54:59 +01:00
|
|
|
(videoUri.getHost().endsWith("vimeo.com") ? "vimeo" : "youtube") +
|
|
|
|
"/play/?video_id=" + videoId;
|
2015-04-08 23:28:19 +02:00
|
|
|
|
2016-10-18 20:06:41 +02:00
|
|
|
} else {
|
|
|
|
videoUrl = videoUri.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
final String fvideoUrl = videoUrl;
|
|
|
|
|
2015-05-27 20:37:22 +02:00
|
|
|
// Check if any video player is active and clear the playlist before queuing if so
|
2015-04-10 20:51:44 +02:00
|
|
|
final HostConnection connection = hostManager.getConnection();
|
|
|
|
final Handler callbackHandler = new Handler();
|
|
|
|
Player.GetActivePlayers getActivePlayers = new Player.GetActivePlayers();
|
|
|
|
getActivePlayers.execute(connection, new ApiCallback<ArrayList<PlayerType.GetActivePlayersReturnType>>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(ArrayList<PlayerType.GetActivePlayersReturnType> result) {
|
|
|
|
boolean videoIsPlaying = false;
|
|
|
|
|
|
|
|
for (PlayerType.GetActivePlayersReturnType player : result) {
|
|
|
|
if (player.type.equals(PlayerType.GetActivePlayersReturnType.VIDEO))
|
|
|
|
videoIsPlaying = true;
|
|
|
|
}
|
|
|
|
|
2015-05-27 20:37:22 +02:00
|
|
|
if (!videoIsPlaying) {
|
|
|
|
// Clear the playlist
|
2016-10-18 20:06:41 +02:00
|
|
|
clearPlaylistAndQueueFile(fvideoUrl, connection, callbackHandler);
|
2015-05-27 20:37:22 +02:00
|
|
|
} else {
|
2016-10-18 20:06:41 +02:00
|
|
|
queueFile(fvideoUrl, false, connection, callbackHandler);
|
2015-05-27 20:37:22 +02:00
|
|
|
}
|
2015-04-10 20:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(int errorCode, String description) {
|
|
|
|
LogUtils.LOGD(TAG, "Couldn't get active player when handling start intent.");
|
|
|
|
Toast.makeText(RemoteActivity.this,
|
|
|
|
String.format(getString(R.string.error_get_active_player), description),
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}, callbackHandler);
|
2015-10-25 21:56:42 +01:00
|
|
|
intent.setAction(null);
|
|
|
|
|
2015-04-08 23:28:19 +02:00
|
|
|
}
|
|
|
|
|
2015-05-27 20:37:22 +02:00
|
|
|
/**
|
|
|
|
* Clears Kodi's playlist, queues the given media file and starts the playlist
|
|
|
|
* @param file File to play
|
|
|
|
* @param connection Host connection
|
|
|
|
* @param callbackHandler Handler to use for posting callbacks
|
|
|
|
*/
|
|
|
|
private void clearPlaylistAndQueueFile(final String file,
|
|
|
|
final HostConnection connection, final Handler callbackHandler) {
|
|
|
|
LogUtils.LOGD(TAG, "Clearing video playlist");
|
|
|
|
Playlist.Clear action = new Playlist.Clear(PlaylistType.VIDEO_PLAYLISTID);
|
|
|
|
action.execute(connection, new ApiCallback<String>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(String result) {
|
|
|
|
// Now queue and start the file
|
|
|
|
queueFile(file, true, connection, callbackHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(int errorCode, String description) {
|
|
|
|
Toast.makeText(RemoteActivity.this,
|
|
|
|
String.format(getString(R.string.error_queue_media_file), description),
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}, callbackHandler);
|
|
|
|
}
|
|
|
|
|
2015-04-10 20:51:44 +02:00
|
|
|
/**
|
2015-05-21 21:08:05 +02:00
|
|
|
* Queues the given media file and optionally starts the playlist
|
2015-04-10 20:51:44 +02:00
|
|
|
* @param file File to play
|
2015-05-21 21:08:05 +02:00
|
|
|
* @param startPlaylist Whether to start playing the playlist after add
|
2015-04-10 20:51:44 +02:00
|
|
|
* @param connection Host connection
|
|
|
|
* @param callbackHandler Handler to use for posting callbacks
|
|
|
|
*/
|
2015-05-27 20:37:22 +02:00
|
|
|
private void queueFile(final String file, final boolean startPlaylist,
|
|
|
|
final HostConnection connection, final Handler callbackHandler) {
|
|
|
|
LogUtils.LOGD(TAG, "Queing file");
|
2015-04-08 23:28:19 +02:00
|
|
|
PlaylistType.Item item = new PlaylistType.Item();
|
|
|
|
item.file = file;
|
2015-04-10 20:51:44 +02:00
|
|
|
Playlist.Add action = new Playlist.Add(PlaylistType.VIDEO_PLAYLISTID, item);
|
|
|
|
action.execute(connection, new ApiCallback<String>() {
|
2015-04-08 23:28:19 +02:00
|
|
|
@Override
|
|
|
|
public void onSuccess(String result ) {
|
2015-05-21 21:08:05 +02:00
|
|
|
if (startPlaylist) {
|
2015-11-10 00:40:48 +01:00
|
|
|
Player.Open action = new Player.Open(Player.Open.TYPE_PLAYLIST, PlaylistType.VIDEO_PLAYLISTID);
|
2015-05-21 21:08:05 +02:00
|
|
|
action.execute(connection, new ApiCallback<String>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(String result) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(int errorCode, String description) {
|
|
|
|
Toast.makeText(RemoteActivity.this,
|
|
|
|
String.format(getString(R.string.error_play_media_file), description),
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}, callbackHandler);
|
|
|
|
}
|
|
|
|
|
2016-03-24 15:26:34 +01:00
|
|
|
refreshPlaylist();
|
2015-04-08 23:28:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onError(int errorCode, String description) {
|
|
|
|
Toast.makeText(RemoteActivity.this,
|
|
|
|
String.format(getString(R.string.error_queue_media_file), description),
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}, callbackHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the YouTube Uri that the YouTube app passes in EXTRA_TEXT
|
|
|
|
* YouTube sends something like: [Video title]: [YouTube URL] so we need
|
|
|
|
* to get the second part
|
|
|
|
*
|
|
|
|
* @param extraText EXTRA_TEXT passed in the intent
|
|
|
|
* @return Uri present in extraText if present
|
|
|
|
*/
|
|
|
|
private Uri getYouTubeUri(String extraText) {
|
|
|
|
if (extraText == null) return null;
|
|
|
|
|
|
|
|
for (String word : extraText.split(" ")) {
|
|
|
|
if (word.startsWith("http://") || word.startsWith("https://")) {
|
|
|
|
try {
|
|
|
|
URL validUri = new URL(word);
|
|
|
|
return Uri.parse(word);
|
|
|
|
} catch (MalformedURLException exc) {
|
|
|
|
LogUtils.LOGD(TAG, "Got a malformed URL in an intent: " + word);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-08 17:54:59 +01:00
|
|
|
* Returns the youtube/vimeo video ID from its URL
|
2015-04-08 23:28:19 +02:00
|
|
|
*
|
2016-01-08 17:54:59 +01:00
|
|
|
* @param playuri Youtube/Vimeo URL
|
|
|
|
* @return Youtube/Vimeo Video ID
|
2015-04-08 23:28:19 +02:00
|
|
|
*/
|
2016-01-08 17:54:59 +01:00
|
|
|
private String getVideoId(Uri playuri) {
|
|
|
|
if (playuri.getHost().endsWith("youtube.com") || playuri.getHost().endsWith("youtu.be") || playuri.getHost().endsWith("vimeo.com")) {
|
2015-04-08 23:28:19 +02:00
|
|
|
// We'll need to get the v= parameter from the URL
|
2016-01-23 12:10:55 +01:00
|
|
|
final Pattern pattern;
|
|
|
|
if (playuri.getHost().endsWith("vimeo.com")) {
|
|
|
|
pattern = Pattern.compile("^(?:https?:\\/\\/)?(?:www\\.|player\\.)?vimeo\\.com\\/(?:.*\\/)?(\\d+)(?:\\?.*)?$",
|
|
|
|
Pattern.CASE_INSENSITIVE);
|
|
|
|
}
|
|
|
|
else {
|
2016-01-24 20:39:13 +01:00
|
|
|
pattern = Pattern.compile("^(?:https?:\\/\\/)?(?:www\\.|m\\.)?youtu(?:\\.be\\/|be\\.com\\/watch\\?v=)([\\w-]+)(?:[\\?&].*)?$",
|
2016-01-23 12:10:55 +01:00
|
|
|
Pattern.CASE_INSENSITIVE);
|
|
|
|
}
|
2015-04-08 23:28:19 +02:00
|
|
|
final Matcher matcher = pattern.matcher(playuri.toString());
|
|
|
|
if (matcher.matches()) {
|
|
|
|
return matcher.group(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-14 12:12:47 +01:00
|
|
|
// Default page change listener, that doesn't scroll images
|
|
|
|
ViewPager.OnPageChangeListener defaultOnPageChangeListener = new ViewPager.OnPageChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageSelected(int position) {
|
|
|
|
setToolbarTitle(toolbar, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrollStateChanged(int state) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets or clear the image background
|
2015-02-15 20:11:32 +01:00
|
|
|
* @param url Image url
|
2015-01-14 12:12:47 +01:00
|
|
|
*/
|
|
|
|
private void setImageViewBackground(String url) {
|
|
|
|
if (url != null) {
|
|
|
|
Point displaySize = new Point();
|
|
|
|
getWindowManager().getDefaultDisplay().getSize(displaySize);
|
|
|
|
|
|
|
|
UIUtils.loadImageIntoImageview(hostManager, url, backgroundImage,
|
|
|
|
displaySize.x, displaySize.y / 2);
|
|
|
|
|
|
|
|
final int pixelsPerPage = displaySize.x / 4;
|
|
|
|
|
|
|
|
backgroundImage.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onPreDraw() {
|
|
|
|
backgroundImage.getViewTreeObserver().removeOnPreDrawListener(this);
|
|
|
|
// Position the image
|
|
|
|
int offsetX = (viewPager.getCurrentItem() - 1) * pixelsPerPage;
|
|
|
|
backgroundImage.scrollTo(offsetX, 0);
|
|
|
|
|
|
|
|
pageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
|
|
|
int offsetX = (int) ((position - 1 + positionOffset) * pixelsPerPage);
|
|
|
|
backgroundImage.scrollTo(offsetX, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageSelected(int position) {
|
|
|
|
setToolbarTitle(toolbar, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrollStateChanged(int state) { }
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
backgroundImage.setImageDrawable(null);
|
|
|
|
pageIndicator.setOnPageChangeListener(defaultOnPageChangeListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HostConnectionObserver.PlayerEventsObserver interface callbacks
|
|
|
|
*/
|
|
|
|
private String lastImageUrl = null;
|
|
|
|
public void playerOnPlay(PlayerType.GetActivePlayersReturnType getActivePlayerResult,
|
|
|
|
PlayerType.PropertyValue getPropertiesResult,
|
|
|
|
ListType.ItemsAll getItemResult) {
|
|
|
|
String imageUrl = (TextUtils.isEmpty(getItemResult.fanart)) ?
|
|
|
|
getItemResult.thumbnail : getItemResult.fanart;
|
|
|
|
if ((imageUrl != null) && !imageUrl.equals(lastImageUrl)) {
|
|
|
|
setImageViewBackground(imageUrl);
|
|
|
|
}
|
|
|
|
lastImageUrl = imageUrl;
|
2015-02-15 20:11:32 +01:00
|
|
|
|
2016-05-20 20:24:34 +02:00
|
|
|
// Start service that manages connection observers
|
|
|
|
LogUtils.LOGD(TAG, "Starting observer service");
|
|
|
|
startService(new Intent(this, ConnectionObserversManagerService.class));
|
|
|
|
|
|
|
|
|
|
|
|
// // Check whether we should show a notification
|
|
|
|
// boolean showNotification = PreferenceManager
|
|
|
|
// .getDefaultSharedPreferences(this)
|
|
|
|
// .getBoolean(Settings.KEY_PREF_SHOW_NOTIFICATION,
|
|
|
|
// Settings.DEFAULT_PREF_SHOW_NOTIFICATION);
|
|
|
|
// if (showNotification) {
|
|
|
|
// // Let's start the notification service
|
|
|
|
// LogUtils.LOGD(TAG, "Starting notification service");
|
|
|
|
// startService(new Intent(this, NotificationObserver.class));
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// // Check whether we should react to phone state changes
|
|
|
|
// boolean shouldPause = PreferenceManager
|
|
|
|
// .getDefaultSharedPreferences(this)
|
|
|
|
// .getBoolean(Settings.KEY_PREF_USE_HARDWARE_VOLUME_KEYS,
|
|
|
|
// Settings.DEFAULT_PREF_USE_HARDWARE_VOLUME_KEYS);
|
|
|
|
// if (shouldPause) {
|
|
|
|
// // Let's start the listening service
|
|
|
|
// LogUtils.LOGD(TAG, "Starting phone state listener");
|
|
|
|
// startService(new Intent(this, PauseCallObserver.class));
|
|
|
|
// }
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void playerOnPause(PlayerType.GetActivePlayersReturnType getActivePlayerResult,
|
|
|
|
PlayerType.PropertyValue getPropertiesResult,
|
|
|
|
ListType.ItemsAll getItemResult) {
|
|
|
|
playerOnPlay(getActivePlayerResult, getPropertiesResult, getItemResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void playerOnStop() {
|
2016-05-20 20:24:34 +02:00
|
|
|
LogUtils.LOGD(TAG, "Player stopping");
|
2015-01-14 12:12:47 +01:00
|
|
|
if (lastImageUrl != null) {
|
|
|
|
setImageViewBackground(null);
|
|
|
|
}
|
|
|
|
lastImageUrl = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void playerNoResultsYet() {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
public void playerOnConnectionError(int errorCode, String description) {
|
|
|
|
playerOnStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void systemOnQuit() {
|
|
|
|
Toast.makeText(this, R.string.xbmc_quit, Toast.LENGTH_SHORT).show();
|
|
|
|
playerOnStop();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void inputOnInputRequested(String title, String type, String value) {
|
|
|
|
SendTextDialogFragment dialog =
|
|
|
|
SendTextDialogFragment.newInstance(title);
|
|
|
|
dialog.show(getSupportFragmentManager(), null);
|
|
|
|
}
|
|
|
|
|
2015-02-15 20:11:32 +01:00
|
|
|
public void observerOnStopObserving() {}
|
|
|
|
|
2015-01-14 12:12:47 +01:00
|
|
|
/**
|
|
|
|
* Now playing fragment listener
|
|
|
|
*/
|
|
|
|
public void SwitchToRemotePanel() {
|
|
|
|
viewPager.setCurrentItem(1);
|
|
|
|
}
|
2016-03-24 15:26:34 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onShuffleClicked() {
|
|
|
|
refreshPlaylist();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void refreshPlaylist() {
|
|
|
|
String tag = "android:switcher:" + viewPager.getId() + ":" + PLAYLIST_FRAGMENT_ID;
|
|
|
|
PlaylistFragment playlistFragment = (PlaylistFragment)getSupportFragmentManager()
|
|
|
|
.findFragmentByTag(tag);
|
|
|
|
if (playlistFragment != null) {
|
|
|
|
playlistFragment.forceRefreshPlaylist();
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 12:12:47 +01:00
|
|
|
}
|