Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.99 KB | None | 0 0
  1. package xxxx.merchant;
  2.  
  3. import android.Manifest;
  4. import android.annotation.SuppressLint;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.content.pm.PackageManager;
  8. import android.content.res.Configuration;
  9. import android.net.Uri;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.os.Environment;
  13. import android.provider.MediaStore;
  14. import android.support.annotation.NonNull;
  15. import android.support.v4.app.ActivityCompat;
  16. import android.support.v4.content.ContextCompat;
  17. import android.support.v7.app.AppCompatActivity;
  18. import android.util.Log;
  19. import android.view.KeyEvent;
  20. import android.view.View;
  21. import android.webkit.ValueCallback;
  22. import android.webkit.WebChromeClient;
  23. import android.webkit.WebSettings;
  24. import android.webkit.WebView;
  25. import android.webkit.WebViewClient;
  26. import android.widget.Toast;
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.text.SimpleDateFormat;
  30. import java.util.Date;
  31.  
  32.  
  33. public class MainActivity extends AppCompatActivity{
  34.     WebView webView;
  35.     private static final String TAG = MainActivity.class.getSimpleName();
  36.     private String mCM;
  37.     private ValueCallback<Uri> mUM;
  38.     private ValueCallback<Uri[]> mUMA;
  39.     private final static int FCR=1;
  40.  
  41.     @Override
  42.     protected void onActivityResult(int requestCode, int resultCode, Intent intent){
  43.         super.onActivityResult(requestCode, resultCode, intent);
  44.         if(Build.VERSION.SDK_INT >= 21){
  45.             Uri[] results = null;
  46.             //Check if response is positive
  47.             if(resultCode== Activity.RESULT_OK){
  48.                 if(requestCode == FCR){
  49.                     if(null == mUMA){
  50.                         return;
  51.                     }
  52.                     if(intent == null){
  53.                         //Capture Photo if no image available
  54.                         if(mCM != null){
  55.                             results = new Uri[]{Uri.parse(mCM)};
  56.                         }
  57.                     }else{
  58.                         String dataString = intent.getDataString();
  59.                         if(dataString != null){
  60.                             results = new Uri[]{Uri.parse(dataString)};
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.             mUMA.onReceiveValue(results);
  66.             mUMA = null;
  67.         }else{
  68.             if(requestCode == FCR){
  69.                 if(null == mUM) return;
  70.                 Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
  71.                 mUM.onReceiveValue(result);
  72.                 mUM = null;
  73.             }
  74.         }
  75.     }
  76.  
  77.     @SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
  78.     @Override
  79.     protected void onCreate(Bundle savedInstanceState){
  80.         super.onCreate(savedInstanceState);
  81.         setContentView(R.layout.activity_main);
  82.  
  83.         if(Build.VERSION.SDK_INT >=23 && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
  84.             ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
  85.         }
  86.  
  87.         webView = (WebView) findViewById(R.id.activity_main_webview);
  88.         assert webView != null;
  89.         WebSettings webSettings = webView.getSettings();
  90.         webSettings.setJavaScriptEnabled(true);
  91.         webSettings.setAllowFileAccess(true);
  92.  
  93.         if(Build.VERSION.SDK_INT >= 21){
  94.             webSettings.setMixedContentMode(0);
  95.             webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  96.         }else if(Build.VERSION.SDK_INT >= 19){
  97.             webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  98.         }else if(Build.VERSION.SDK_INT < 19){
  99.             webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  100.         }
  101.         webView.setWebViewClient(new Callback());
  102.         webView.loadUrl("xxxxxxx");
  103.         webView.setWebChromeClient(new WebChromeClient(){
  104.  
  105.             //For Android 5.0+
  106.             public boolean onShowFileChooser(
  107.                     WebView webView, ValueCallback<Uri[]> filePathCallback,
  108.                     WebChromeClient.FileChooserParams fileChooserParams){
  109.                 if(mUMA != null){
  110.                     mUMA.onReceiveValue(null);
  111.                 }
  112.                 mUMA = filePathCallback;
  113.                 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  114.                 if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
  115.                     File photoFile = null;
  116.                     try{
  117.                         photoFile = createImageFile();
  118.                         takePictureIntent.putExtra("PhotoPath", mCM);
  119.                     }catch(IOException ex){
  120.                         Log.e(TAG, "Image file creation failed", ex);
  121.                     }
  122.                     if(photoFile != null){
  123.                         mCM = "file:" + photoFile.getAbsolutePath();
  124.                         takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  125.  
  126.                     }else{
  127.                         takePictureIntent = null;
  128.                     }
  129.                 }
  130.                 Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  131.                 contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  132.                 contentSelectionIntent.setType("*/*");
  133.                 // PRueba con Android Gus
  134.                 contentSelectionIntent.putExtra("crop", "true");
  135.                 contentSelectionIntent.putExtra("outputX", 150);
  136.                 contentSelectionIntent.putExtra("outputY", 150);
  137.                 contentSelectionIntent.putExtra("aspectX", 1);
  138.                 contentSelectionIntent.putExtra("aspectY", 1);
  139.                 contentSelectionIntent.putExtra("scale", true);
  140.                 // Fin prueba - Borrar si no funciona
  141.                 Intent[] intentArray;
  142.                 if(takePictureIntent != null){
  143.                     intentArray = new Intent[]{takePictureIntent};
  144.                 }else{
  145.                     intentArray = new Intent[0];
  146.                 }
  147.  
  148.                 Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  149.                 chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  150.                 chooserIntent.putExtra(Intent.EXTRA_TITLE, "Seleccionar imagen");
  151.                 chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  152.                 startActivityForResult(chooserIntent, FCR);
  153.                 return true;
  154.             }
  155.         });
  156.     }
  157.     public class Callback extends WebViewClient{
  158.         public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
  159.             Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
  160.         }
  161.     }
  162.     // Create an image file
  163.     private File createImageFile() throws IOException{
  164.         @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  165.         String imageFileName = "img_"+timeStamp+"_";
  166.         File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  167.         return File.createTempFile(imageFileName,".jpg",storageDir);
  168.     }
  169.     @Override
  170.     public boolean onKeyDown(int keyCode, @NonNull KeyEvent event){
  171.         if(event.getAction() == KeyEvent.ACTION_DOWN){
  172.             switch(keyCode){
  173.                 case KeyEvent.KEYCODE_BACK:
  174.                     if(webView.canGoBack()){
  175.                         webView.goBack();
  176.                     }else{
  177.                         finish();
  178.                     }
  179.                     return true;
  180.             }
  181.         }
  182.         return super.onKeyDown(keyCode, event);
  183.     }
  184.  
  185.     @Override
  186.     public void onConfigurationChanged(Configuration newConfig){
  187.         super.onConfigurationChanged(newConfig);
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement