Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package org.apache.cordova.plugin;
  2.  
  3. import org.apache.cordova.CordovaPlugin;
  4. import org.apache.cordova.CallbackContext;
  5. import org.apache.cordova.engine.SystemWebView;
  6.  
  7. import org.json.JSONArray;
  8. import org.json.JSONException;
  9. import org.json.JSONObject;
  10.  
  11. import android.content.ActivityNotFoundException;
  12. import android.content.Context;
  13. import android.content.Intent;
  14. import android.util.Log;
  15.  
  16.  
  17. /**
  18.  * * This class provides methods for AndroidTV.
  19.  * */
  20. public class AndroidTV extends CordovaPlugin {
  21.     @Override
  22.     public void onNewIntent(Intent intent) {
  23.         super.onNewIntent(intent);
  24.         Log.e("onNewIntent", "action: " + intent.getAction() + "\r\ndata: " + intent.getData() + "\r\nscheme: " + intent.getScheme());
  25.     }
  26.  
  27.     @Override
  28.     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  29.         Log.e("AndroidTV-execute", "action: " + action + "\r\n" + "args: " + args.toString());
  30.         if (action.equals("getStringExtra")) {
  31.             String message = args.getString(0);
  32.             this.getActivityStringExtra(message, callbackContext);
  33.             return true;
  34.         } else if (action.equals("ftiToScreen")) {
  35.             this.ftiToScreen(callbackContext);
  36.             return true;
  37.         } else if (action.equals("setSize")) {
  38.             int width = args.getInt(0);
  39.             int height = args.getInt(1);
  40.             this.setSize(width, height, callbackContext);
  41.             return true;
  42.         }
  43.         return false;
  44.     }
  45.  
  46.     private void setSize(int width, int height, CallbackContext callbackContext) {
  47.         final Double val = 1080.0 / height * 100;
  48.         final SystemWebView webView = (SystemWebView)super.webView.getEngine().getView();
  49.  
  50.         webView.post(new Runnable() {
  51.             @Override
  52.             public void run() {
  53.                 webView.setPadding(0, 0, 0, 0);
  54.                 webView.setInitialScale(val.intValue());
  55.             }
  56.         });
  57.         callbackContext.success();
  58.     }
  59.  
  60.     private void ftiToScreen(CallbackContext callbackContext) {
  61.         final SystemWebView webView = (SystemWebView)super.webView.getEngine().getView();
  62.  
  63.         webView.post(new Runnable() {
  64.             @Override
  65.             public void run() {
  66.                 webView.getSettings().setLoadWithOverviewMode(true);
  67.                 webView.getSettings().setUseWideViewPort(true);
  68.             }
  69.         });
  70.         callbackContext.success();
  71.     }
  72.  
  73.     private void getActivityStringExtra(String extraString, CallbackContext callbackContext) {
  74.         Intent intent = this.cordova.getActivity().getIntent();
  75.         Log.d("AndroidTVPlugin", "try to get string extra: " + extraString);
  76.         try {
  77.             callbackContext.success(intent.getStringExtra(extraString));
  78.         } catch(ActivityNotFoundException e) {
  79.             callbackContext.error("Failed to get stringExtra");
  80.         }
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement