Skip to content

Instantly share code, notes, and snippets.

@lisawray
Last active August 29, 2015 14:21
Show Gist options
  • Save lisawray/a833f5512a085e957e97 to your computer and use it in GitHub Desktop.
Save lisawray/a833f5512a085e957e97 to your computer and use it in GitHub Desktop.
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.embedly.api.Api;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.HashMap;
public class EmbedlyActivity extends Activity {
private static final String TAG = "EmbedlyActivity";
private static final int FLAGS = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
private static final String EMBEDLY_USER_AGENT = "embedly-ios/1.0";
private static final String errorHtml = ""; // Can put an error message here if we want
private static final String apiKey = "API KEY HERE";
String template = "<html>\n" +
" <head>\n" +
" <style>\n" +
" %s\n" +
" </style>\n" +
" </head>\n" +
" <body>\n" +
" <div class=\"video-container\">%s</div>\n" +
" </body>\n" +
"</html>";
String css =
"body {\n" +
" margin: 0;\n" +
" padding: 0;\n" +
"}\n" +
"\n" +
".video-container {\n" +
" position: relative;\n" +
" padding-bottom: 56.25%;\n" +
" padding-top: 35px;\n" +
" height: 0;\n" +
" overflow: hidden;\n" +
"}\n" +
"\n" +
".video-container iframe {\n" +
" position: absolute;\n" +
" top:0;\n" +
" margin:0;\n" +
" padding:0;\n" +
" left: 0;\n" +
" width: 100%;\n" +
" height: 100%;\n" +
"}";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen_embedly);
final WebView webView = (WebView) findViewById(R.id.player);
webView.setSystemUiVisibility(FLAGS);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Retrieve URL passed to the activity
String url = getIntent().getStringExtra(YouTubeActivity.KEY_VIDEO_URL);
AsyncTask<String, Void, String> loadEmbedlyDataTask = new AsyncTask<String, Void, String>() {
@Override protected String doInBackground(String... urls) {
final Api api = new Api(EMBEDLY_USER_AGENT, apiKey);
final HashMap<String, Object> params = new HashMap<String, Object>();
params.put("url", urls[0]);
params.put("scheme", "https");
final JSONArray response = api.oembed(params);
String embedlyHtml = errorHtml;
try {
// In our case, we're only interested in videos
embedlyHtml = response.getJSONObject(0).getString("html");
} catch (JSONException e) {
Log.e(TAG, "Error extracting HTML from Embedly JSON", e);
}
String html = String.format(template, css, embedlyHtml);
return html;
}
@Override protected void onPostExecute(String html) {
webView.loadData(html, "text/html", "utf-8");
}
};
// Launch the task.
loadEmbedlyDataTask.execute(url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment