title: Deep Linking in Webview
body: <p>I've been developing a WebView app for the Stony Brook chapter of SHPE and I'm trying to implement deep linking where if a user clicks or opens a URL in the browser it will open in the app. I've got it all working, even file uploading for the job postings section, and I disabled ajax on the site since it was causing issues. Caching I'm not sure is actually functioning, but for right now, I just want to get deep linking working, because the site uses OneSignal for notifications and they just open in the browser when tapped. If I could capture the URL and open any from stonybrookshpe.org in the app, it would solve my problem, but I'm stuck, thank you. Heres my MainActivity.java</p>
  
  <pre><code>package org.stonybrookshpe.stonybrookshpe;
  
  import android.Manifest;
  
  import android.annotation.SuppressLint;
  
  import android.app.Activity;
  
  import android.content.Intent;
  
  import android.content.pm.PackageManager;
  
  import android.content.res.Configuration;
  
  import android.net.ConnectivityManager;
  
  import android.net.Uri;
  
  import android.os.Build;
  
  import android.os.Bundle;
  
  import android.os.Environment;
  
  import android.provider.MediaStore;
  
  import android.support.annotation.NonNull;
  
  import android.support.v4.app.ActivityCompat;
  
  import android.support.v4.content.ContextCompat;
  
  import android.support.v7.app.AppCompatActivity;
  
  import android.util.Log;
  
  import android.view.KeyEvent;
  
  import android.view.View;
  
  import android.webkit.ValueCallback;
  
  import android.webkit.WebChromeClient;
  
  import android.webkit.WebSettings;
  
  import android.webkit.WebView;
  
  import android.webkit.WebViewClient;
  
  import android.webkit.WebResourceError;
  
  import android.webkit.WebResourceRequest;
  
  import java.io.File;
  
  import java.io.IOException;
  
  import java.text.SimpleDateFormat;
  
  import java.util.Date;
  
  import com.onesignal.OneSignal;
  
  
  public class MainActivity extends AppCompatActivity {
  
  
      WebView webView;
  
      ConnectivityManager cM;
  
      private static final String TAG = MainActivity.class.getSimpleName();
  
      private String mCM;
  
      private ValueCallback&lt;Uri&gt; mUM;
  
      private ValueCallback&lt;Uri[]&gt; mUMA;
  
      private final static int FCR = 1;
  
  
      @Override
  
      protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  
          super.onActivityResult(requestCode, resultCode, intent);
          if (Build.VERSION.SDK_INT &gt;= 21) {
  
              Uri[] results = null;
  
              //Check if response is positive
  
              if (resultCode == Activity.RESULT_OK) {
  
                  if (requestCode == FCR) {
  
                      if (null == mUMA) {
  
                          return;
  
                      }
  
                      if (intent == null || intent.getData() == null) {
  
                          //Capture Photo if no image available
  
                          if (mCM != null) {
  
                              results = new Uri[]{Uri.parse(mCM)};
  
                          }
  
                      } else {
  
                          String dataString = intent.getDataString();
  
                          if (dataString != null) {
  
                              results = new Uri[]{Uri.parse(dataString)};
  
                          }
  
                      }
  
                  }
  
              }
  
              mUMA.onReceiveValue(results);
  
              mUMA = null;
  
          } else {
  
              if (requestCode == FCR) {
  
                  if (null == mUM) return;
  
                  Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
  
                  mUM.onReceiveValue(result);
  
                  mUM = null;
  
              }
  
          }
  
      }
  
  
      @SuppressLint({"SetJavaScriptEnabled", "WrongViewCast"})
  
      @Override
  
      protected void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
  
  
          // OneSignal Initialization
  
          OneSignal.startInit(this)
  
                  .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
  
                  .unsubscribeWhenNotificationsAreDisabled(true)
  
                  .init();
  
          OneSignal.setEmail("sbshpe@gmail.com");
  
  
          setContentView(R.layout.activity_main);
  
          if (Build.VERSION.SDK_INT &gt;= 23 &amp;&amp; (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)) {
  
              ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, 1);
  
          }
  
          webView = (WebView) findViewById(R.id.ifView);
  
          assert webView != null;
  
          WebSettings webSettings = webView.getSettings();
  
          webSettings.setJavaScriptEnabled(true);
  
          webSettings.setAllowFileAccess(true);
  
          webSettings.setAllowFileAccess(true);
  
          if (Build.VERSION.SDK_INT &gt;= 21) {
  
              webSettings.setMixedContentMode(0);
  
              webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  
          } else if (Build.VERSION.SDK_INT &gt;= 19) {
  
              webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
  
          } else if (Build.VERSION.SDK_INT &lt; 19) {
  
              webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
  
          }
  
          webView.setWebViewClient(new Callback());
  
  
          cM = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
  
          if(cM != null &amp;&amp; cM.getActiveNetworkInfo() != null &amp;&amp; cM.getActiveNetworkInfo().isConnected()){
  
              webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
  
              webView.loadUrl("http://stonybrookshpe.org");
  
          }
  
          else{
  
              webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
  
              webView.loadUrl("http://stonybrookshpe.org");
  
          }
  
          webView.setWebChromeClient(new WebChromeClient() {
  
              //For Android 3.0+
  
              public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg) {
  
                  mUM = uploadMsg;
  
                  Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  
                  i.addCategory(Intent.CATEGORY_OPENABLE);
  
                  i.setType("*/*");
  
                  MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FCR);
  
              }
  
              // For Android 3.0+, above method not supported in some android 3+ versions, in such case we use this
  
              public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
  
                  mUM = uploadMsg;
  
                  Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  
                  i.addCategory(Intent.CATEGORY_OPENABLE);
  
                  i.setType("*/*");
  
                  MainActivity.this.startActivityForResult(
  
                          Intent.createChooser(i, "File Browser"),
  
                          FCR);
  
              }
  
              //For Android 4.1+
  
              public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg, String acceptType, String capture) {
  
                  mUM = uploadMsg;
  
                  Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  
                  i.addCategory(Intent.CATEGORY_OPENABLE);
  
                  i.setType("*/*");
  
                  MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FCR);
  
              }
  
              //For Android 5.0+
  
              public boolean onShowFileChooser(
  
                      WebView webView, ValueCallback&lt;Uri[]&gt; filePathCallback,
  
                      WebChromeClient.FileChooserParams fileChooserParams) {
  
                  if (mUMA != null) {
  
                      mUMA.onReceiveValue(null);
  
                  }
  
                  mUMA = filePathCallback;
  
                  Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  
                  if (takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
  
                      File photoFile = null;
  
                      try {
  
                          photoFile = createImageFile();
  
                          takePictureIntent.putExtra("PhotoPath", mCM);
  
                      } catch (IOException ex) {
  
                          Log.e(TAG, "Image file creation failed", ex);
  
                      }
  
                      if (photoFile != null) {
  
                          mCM = "file:" + photoFile.getAbsolutePath();
  
                          takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  
                      } else {
  
                          takePictureIntent = null;
  
                      }
  
                  }
  
                  Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
  
                  contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
  
                  contentSelectionIntent.setType("*/*");
  
                  Intent[] intentArray;
  
                  if (takePictureIntent != null) {
  
                      intentArray = new Intent[]{takePictureIntent};
  
                  } else {
  
                      intentArray = new Intent[0];
  
                  }
  
  
                  Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
  
                  chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
  
                  chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
  
                  chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
  
                  startActivityForResult(chooserIntent, FCR);
  
                  return true;
  
              }
  
          });
  
      }
  
      public class Callback extends WebViewClient {
  
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
  
  
              /*
             Log.i(TAG, "shouldOverrideUrlLoading: " + url);
              Intent intent;
  
  
               if (url.contains(AppConstants.http://stonybrookshpe.org)) {
             if (Uri.parse(url).getHost().equals("stonybrookshpe.org")) {
  
                  view.loadUrl(url);
  
                  return true;
             }
  */
  
  
              if (url.startsWith("mailto:")) {
                  // We use `ACTION_SENDTO` instead of `ACTION_SEND` so that only email programs are launched.
                  Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
  
                  // Parse the url and set it as the data for the `Intent`.
                  emailIntent.setData(Uri.parse(url));
  
                  // `FLAG_ACTIVITY_NEW_TASK` opens the email program in a new task instead as part of this application.
                  emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
                  // Make it so.
                  startActivity(emailIntent);
  
                  return true;
  
              }
  
              else {
  
                      if (Uri.parse(url).getHost().equals("stonybrookshpe.org")) {
  
                          return false;
  
                      }
  
              }
  
  
              return true;
              /*
              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
              startActivity(intent);
              return true;
  */
  
  
          }
  
  
          public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  
              String html = "";
              String mime = "text/html";
              String encoding = "utf-8";
  
              //webview myWebView = (WebView)this.findViewById(R.id.ifView);
              webView.loadDataWithBaseURL(null, html, mime, encoding, null);
  
          }
  
      }
  
      // Create an image file
  
      private File createImageFile() throws IOException {
  
          @SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
  
          String imageFileName = "img_" + timeStamp + "_";
  
          File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  
          return File.createTempFile(imageFileName, ".jpg", storageDir);
  
      }
  
      @Override
  
      public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
  
          if (event.getAction() == KeyEvent.ACTION_DOWN) {
  
              switch (keyCode) {
  
                  case KeyEvent.KEYCODE_BACK:
  
                      if (webView.canGoBack()) {
  
                          webView.goBack();
  
                      } else {
  
                          finish();
  
                      }
  
                      return true;
  
              }
  
          }
  
          return super.onKeyDown(keyCode, event);
  
      }
  
  
      /*
      @Override
  
      public void onConfigurationChanged(Configuration newConfig) {
  
          super.onConfigurationChanged(newConfig);
  
      }
      */
  
  }
  </code></pre>
  
  <p>and my AndroidManifest.xml</p>
  
  <pre><code>    &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  
      package="org.stonybrookshpe.stonybrookshpe"&gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /&gt;
  
  
      &lt;application
  
          android:allowBackup="true"
  
          android:icon="@mipmap/ic_launcher"
  
          android:label="@string/app_name"
  
          android:roundIcon="@mipmap/ic_launcher_round"
  
          android:supportsRtl="true"
  
          android:theme="@style/StonyBrookSHPE"&gt;
  
          &lt;activity android:name=".MainActivity"
  
              android:theme="@style/StonyBrookSHPE"&gt;
  
              &lt;intent-filter&gt;
  
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
  
              &lt;/intent-filter&gt;
  
              &lt;intent-filter&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
  
  
                  &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
  
                  &lt;!-- Accepts URIs that begin with "http://www.example.com/gizmos” --&gt;
  
                  &lt;data android:scheme="http"
  
                      android:host="stonybrookshpe.org"
  
                      android:pathPrefix="/" /&gt;
  
                  &lt;!-- note that the leading "/" is required for pathPrefix--&gt;
  
              &lt;/intent-filter&gt;
  
          &lt;/activity&gt;
  
      &lt;/application&gt;
  
  
  &lt;/manifest&gt;
  </code></pre>