Как использовать правильные намерения для WebView при угоне схемы уязвимых намерений - PullRequest
2 голосов
/ 26 марта 2019

У меня есть этот WebView, в котором я открываю страницу, на которой есть ссылки, чтобы открыть WhatsApp, Messenger и телефонный звонок, который не открывался бы, если бы его не было в браузере.

Google явно не понравилось и говорит, что я должен пропустить эту часть.

Есть ли способ избавиться от этого?

Исходный код прокомментирован, мое приложение открыто в веб-навигаторе, и я не хочу его. поэтому я использовал схему намерений, чтобы открыть любую ссылку на странице.

public class MainActivity extends AppCompatActivity {

WebView wv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     wv = findViewById(R.id.web);
    wv.setWebViewClient(new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){

           /* if (url.startsWith("tel:") || url.startsWith("whatsapp:") || url.startsWith("intent")){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                //view.reload();
                return true;
            }

            view.loadUrl(url);
            return true;*/
           /* if (url.startsWith("http:") || url.startsWith("https:")) {

                return  false;
            }
            //agregar validacion para email, telefono y whatsapp
            if(url.startsWith("tel://")|| url.startsWith("mailto://")|| url.startsWith("whatsapp://")){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));


            }*/

            if (url.startsWith("http")) return false;//open web links as usual
            //try to find browse activity to handle uri
            Uri parsedUri = Uri.parse(url);
            PackageManager packageManager = getApplicationContext().getPackageManager();
            Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);
            if (browseIntent.resolveActivity(packageManager) != null) {
                getApplicationContext().startActivity(browseIntent);
                return true;
            }
            //if not activity found, try to parse intent://
            if (url.startsWith("intent:")) {
                try {
                    Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                    if (intent.resolveActivity(getApplicationContext().getPackageManager()) != null) {
                        getApplicationContext().startActivity(intent);
                        return true;
                    }
                    //try to find fallback url
                    String fallbackUrl = intent.getStringExtra("browser_fallback_url");
                    if (fallbackUrl != null) {
                        wv.loadUrl(fallbackUrl);
                        return true;
                    }
                    //invite to install
                    Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(
                            Uri.parse("market://details?id=" + intent.getPackage()));
                    if (marketIntent.resolveActivity(packageManager) != null) {
                        getApplicationContext().startActivity(marketIntent);
                        return true;
                    }
                } catch (URISyntaxException e) {
                    //not an intent uri
                }
            }
            return true;
        }

    });

    wv.getSettings().setJavaScriptEnabled(true);
    // wv.getSettings().setLoadWithOverviewMode(true);

    wv.loadUrl("http://www.saborasonoyta.com");

}


protected void showAppExitDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    builder.setTitle("POR FAVOR, CONFIRME");
    builder.setMessage("desea salir de la aplicación?");
    builder.setCancelable(true);

    builder.setPositiveButton("Si", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Do something when user want to exit the app
            // Let allow the system to handle the event, such as exit the app
            MainActivity.super.onBackPressed();
        }
    });

    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            // Do something when want to stay in the app
            Toast.makeText(getApplicationContext(),"Gracias",Toast.LENGTH_LONG).show();
        }
    });

    // Create the alert dialog using alert dialog builder
    AlertDialog dialog = builder.create();

    // Finally, display the dialog when user press back button
    dialog.show();
}

@Override
public void onBackPressed(){
    if(wv.canGoBack()){
        // If web view have back history, then go to the web view back history
        wv.goBack();
    }else {
        // Ask the user to exit the app or stay in here
        showAppExitDialog();
    }
  }
}
...