show inline youtube player (if app installed)

minor bugfixes
This commit is contained in:
Andrew Dolgov 2015-07-11 12:27:29 +03:00
parent c79fb5a1b2
commit 54488e108a
8 changed files with 211 additions and 5 deletions

View File

@ -38,4 +38,5 @@ dependencies {
compile 'com.viewpagerindicator:library:2.4.1'
compile 'com.nhaarman.listviewanimations:lib-core:3.1.0@aar'
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/YouTubeAndroidPlayerApi.jar')
}

Binary file not shown.

View File

@ -92,6 +92,7 @@
<orderEntry type="library" exported="" name="nineoldandroids-2.4.0" level="project" />
<orderEntry type="library" exported="" name="gson-2.3" level="project" />
<orderEntry type="library" exported="" name="acra-4.5.0" level="project" />
<orderEntry type="library" exported="" name="YouTubeAndroidPlayerApi" level="project" />
<orderEntry type="library" exported="" name="support-annotations-22.2.0" level="project" />
<orderEntry type="library" exported="" name="support-v4-22.2.0" level="project" />
<orderEntry type="library" exported="" name="circleindicator-1.1.1" level="project" />

View File

@ -267,6 +267,11 @@
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/title_activity_video_player" >
</activity>
<activity
android:name=".YoutubePlayerActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/title_activity_video_player" >
</activity>
</application>
</manifest>

View File

@ -4,6 +4,7 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources.Theme;
import android.graphics.Bitmap;
import android.graphics.Paint;
@ -76,6 +77,7 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -693,6 +695,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
private final DisplayImageOptions displayImageOptions;
boolean showFlavorImage;
private int m_minimumHeightToEmbed;
boolean m_youtubeInstalled;
public ArticleListAdapter(Context context, int textViewResourceId, ArrayList<Article> items) {
super(context, textViewResourceId, items);
@ -718,6 +721,13 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
.displayer(new FadeInBitmapDisplayer(500))
.build();
List<ApplicationInfo> packages = m_activity.getPackageManager().getInstalledApplications(0);
for (ApplicationInfo pi : packages) {
if (pi.packageName.equals("com.google.android.youtube")) {
m_youtubeInstalled = true;
break;
}
}
}
public int getViewTypeCount() {
@ -1191,7 +1201,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
Matcher matcher = pattern.matcher(srcEmbed);
if (matcher.find()) {
String vid = matcher.group(1);
final String vid = matcher.group(1);
final String thumbUri = "http://img.youtube.com/vi/"+vid+"/mqdefault.jpg";
final String videoUri = "https://youtu.be/" + vid;
@ -1257,9 +1267,19 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener,
holder.flavorImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(videoUri));
startActivity(intent);
if (m_youtubeInstalled) {
Intent intent = new Intent(m_activity, YoutubePlayerActivity.class);
intent.putExtra("streamUri", videoUri);
intent.putExtra("vid", vid);
intent.putExtra("title", article.title);
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(videoUri));
startActivity(intent);
}
}
});
}

View File

@ -238,7 +238,6 @@ public class VideoPlayerActivity extends CommonActivity {
if (m_streamUri != null) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/mp4");
intent.putExtra(Intent.EXTRA_SUBJECT, m_streamUri);
intent.putExtra(Intent.EXTRA_TEXT, m_streamUri);

View File

@ -0,0 +1,150 @@
package org.fox.ttrss;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
public class YoutubePlayerActivity extends CommonActivity implements YouTubePlayer.OnInitializedListener {
private final String TAG = this.getClass().getSimpleName();
private static final String DEVELOPER_KEY = "AIzaSyD8BS4Uj21jg_gHZfP4v0VXrAWiwqd05nk";
private String m_streamUri;
private String m_videoId;
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.DarkTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube_player);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (!isPortrait())
getSupportActionBar().hide();
setTitle(getIntent().getStringExtra("title"));
if (savedInstanceState == null) {
m_streamUri = getIntent().getStringExtra("streamUri");
m_videoId = getIntent().getStringExtra("vid");
} else {
m_streamUri = savedInstanceState.getString("streamUri");
m_videoId = savedInstanceState.getString("vid");
}
YouTubePlayerSupportFragment frag = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_youtube_player);
frag.initialize(DEVELOPER_KEY, this);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (!isPortrait())
getSupportActionBar().hide();
else
getSupportActionBar().show();
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putString("streamUri", m_streamUri);
out.putString("vid", m_videoId);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_video_player, menu);
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.activity_video_player, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return onContextItemSelected(item); // this is really bad :()
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.article_vid_open:
if (m_streamUri != null) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(m_streamUri));
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
toast(R.string.error_other_error);
}
}
return true;
case R.id.article_vid_share:
if (m_streamUri != null) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, m_streamUri);
intent.putExtra(Intent.EXTRA_TEXT, m_streamUri);
startActivity(Intent.createChooser(intent, m_streamUri));
}
return true;
default:
Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId());
return super.onContextItemSelected(item);
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
Log.d(TAG, "youtube: init success");
findViewById(R.id.video_loading).setVisibility(View.GONE);
if (!wasRestored) {
player.cueVideo(m_videoId);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
Log.d(TAG, "youtube: init failure");
findViewById(R.id.video_loading).setVisibility(View.GONE);
toast(result.toString());
}
}

View File

@ -0,0 +1,30 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="?android:colorBackground"
tools:context="org.fox.ttrss.VideoPlayerActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar">
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/fragment_youtube_player"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/video_loading"
android:layout_gravity="center"
android:indeterminate="true" />
</FrameLayout>
<include layout="@layout/toolbar" android:id="@+id/toolbar" />
</RelativeLayout>