44import android .content .Context ;
55import android .content .DialogInterface ;
66import android .content .Intent ;
7+ import android .content .SharedPreferences ;
78import android .net .Uri ;
9+ import android .preference .Preference ;
810import android .preference .PreferenceManager ;
911import android .support .v4 .view .MenuItemCompat ;
1012import android .support .v7 .app .ActionBar ;
4042
4143public class ActionBarHandler {
4244 private static final String TAG = ActionBarHandler .class .toString ();
45+ private static final String KORE_PACKET = "org.xbmc.kore" ;
46+
4347 private static ActionBarHandler handler = null ;
4448
4549 private Context context = null ;
@@ -49,6 +53,8 @@ public class ActionBarHandler {
4953 private int selectedStream = -1 ;
5054 private String videoTitle = "" ;
5155
56+ SharedPreferences defaultPreferences = null ;
57+
5258 public static ActionBarHandler getHandler () {
5359 if (handler == null ) {
5460 handler = new ActionBarHandler ();
@@ -73,7 +79,7 @@ public void setStreams(VideoInfo.Stream[] streams) {
7379 this .streams = streams ;
7480 selectedStream = 0 ;
7581 String [] itemArray = new String [streams .length ];
76- String defaultResolution = PreferenceManager . getDefaultSharedPreferences ( context )
82+ String defaultResolution = defaultPreferences
7783 .getString (context .getString (R .string .defaultResolutionPreference ),
7884 context .getString (R .string .defaultResolutionListItem ));
7985 int defaultResolutionPos = 0 ;
@@ -104,15 +110,21 @@ public boolean setupMenu(Menu menu, MenuInflater inflater, Context context) {
104110 // CAUTION set item properties programmatically otherwise it would not be accepted by
105111 // appcompat itemsinflater.inflate(R.menu.videoitem_detail, menu);
106112
113+ defaultPreferences = PreferenceManager .getDefaultSharedPreferences (context );
114+
107115 inflater .inflate (R .menu .videoitem_detail , menu );
108116 MenuItem playItem = menu .findItem (R .id .menu_item_play );
109117 MenuItem shareItem = menu .findItem (R .id .menu_item_share );
118+ MenuItem castItem = menu .findItem (R .id .action_play_with_kodi );
110119
111120 MenuItemCompat .setShowAsAction (playItem , MenuItemCompat .SHOW_AS_ACTION_ALWAYS
112121 | MenuItemCompat .SHOW_AS_ACTION_WITH_TEXT );
113122 MenuItemCompat .setShowAsAction (shareItem , MenuItemCompat .SHOW_AS_ACTION_IF_ROOM
114123 | MenuItemCompat .SHOW_AS_ACTION_WITH_TEXT );
115124
125+ castItem .setVisible (defaultPreferences
126+ .getBoolean (context .getString (R .string .showPlayWidthKodiPreference ), false ));
127+
116128 return true ;
117129 }
118130
@@ -143,6 +155,10 @@ public boolean onItemSelected(MenuItem item, Context context) {
143155 Intent intent = new Intent (context , SettingsActivity .class );
144156 context .startActivity (intent );
145157 }
158+ break ;
159+ case R .id .action_play_with_kodi :
160+ playWithKodi ();
161+ break ;
146162 default :
147163 Log .e (TAG , "Menu Item not known" );
148164 }
@@ -216,8 +232,7 @@ public void downloadVideo() {
216232 DownloadManager .Request request = new DownloadManager .Request (
217233 Uri .parse (streams [selectedStream ].url ));
218234 request .setDestinationUri (Uri .fromFile (new File (
219- PreferenceManager .getDefaultSharedPreferences (context )
220- .getString ("download_path_preference" , "/storage/emulated/0/NewPipe" )
235+ defaultPreferences .getString ("download_path_preference" , "/storage/emulated/0/NewPipe" )
221236 + "/" + videoTitle + suffix )));
222237 try {
223238 dm .enqueue (request );
@@ -236,4 +251,35 @@ public void openInBrowser() {
236251 context .startActivity (Intent .createChooser (intent , context .getString (R .string .chooseBrowser )));
237252 }
238253 }
254+
255+ public void playWithKodi () {
256+ if (!videoTitle .isEmpty ()) {
257+ try {
258+ Intent intent = new Intent (Intent .ACTION_VIEW );
259+ intent .setPackage (KORE_PACKET );
260+ intent .setData (Uri .parse (webisteUrl .replace ("https" , "http" )));
261+ context .startActivity (intent );
262+ } catch (Exception e ) {
263+ e .printStackTrace ();
264+ AlertDialog .Builder builder = new AlertDialog .Builder (context );
265+ builder .setMessage (R .string .koreNotFound )
266+ .setPositiveButton (R .string .installeKore , new DialogInterface .OnClickListener () {
267+ @ Override
268+ public void onClick (DialogInterface dialog , int which ) {
269+ Intent intent = new Intent ();
270+ intent .setAction (Intent .ACTION_VIEW );
271+ intent .setData (Uri .parse (context .getString (R .string .fdroidKoreUrl )));
272+ context .startActivity (intent );
273+ }
274+ })
275+ .setNegativeButton (R .string .cancel , new DialogInterface .OnClickListener () {
276+ @ Override
277+ public void onClick (DialogInterface dialog , int which ) {
278+
279+ }
280+ });
281+ builder .create ().show ();
282+ }
283+ }
284+ }
239285}
0 commit comments