AI2 Component  (Version nb184)
SplashActivity.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2018-2020 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
8 import android.app.Activity;
9 
10 import android.content.Context;
11 import android.content.Intent;
12 import android.content.pm.PackageInfo;
13 import android.content.pm.PackageManager;
14 import android.content.pm.PackageManager.NameNotFoundException;
15 
16 import android.net.Uri;
17 
18 import android.os.Bundle;
19 import android.os.Handler;
20 
21 import android.util.Log;
22 
23 import android.webkit.JavascriptInterface;
24 import android.webkit.WebChromeClient;
25 import android.webkit.WebSettings;
26 import android.webkit.WebStorage;
27 import android.webkit.WebView;
28 import android.webkit.WebViewClient;
29 
30 import androidx.core.app.ActivityCompat;
31 import androidx.core.content.ContextCompat;
32 
34 
40 public final class SplashActivity extends AppInventorCompatActivity {
41 
42  WebView webview;
43  Handler handler;
44 
45  public class JavaInterface {
46  Context mContext;
47 
48  public JavaInterface(Context context) {
49  mContext = context;
50  }
51 
52  @JavascriptInterface
53  public boolean hasPermission(String permission) {
54  if (SdkLevel.getLevel() < SdkLevel.LEVEL_MARSHMALLOW) { // permissions granted at install prior to Marshmallow
55  return true;
56  } else if (ContextCompat.checkSelfPermission(mContext, permission) ==
57  PackageManager.PERMISSION_GRANTED) {
58  return true;
59  } else {
60  return false;
61  }
62  }
63 
64  @JavascriptInterface
65  public void askPermission(String permission) {
66  ActivityCompat.requestPermissions((Activity) SplashActivity.this,
67  new String[] { permission}, 1);
68  }
69 
70  @JavascriptInterface
71  public String getVersion() {
72  try {
73  String packageName = mContext.getPackageName();
74  PackageInfo pInfo = mContext.getPackageManager().getPackageInfo(packageName, 0);
75  return (pInfo.versionName);
76  } catch (NameNotFoundException e) {
77  return "Unknown";
78  }
79  }
80 
81  @JavascriptInterface
82  public void finished() {
83  SplashActivity.this.handler.post(new Runnable() {
84  @Override
85  public void run() {
86  SplashActivity.this.webview.destroy();
87  SplashActivity.this.finish();
88  }
89  });
90  }
91  }
92 
93  @Override
94  public void onCreate(Bundle savedInstanceState) {
95  super.onCreate(savedInstanceState);
96  JavaInterface android = new JavaInterface(this);
97  handler = new Handler();
98  webview = new WebView(this);
99  WebSettings webSettings = webview.getSettings();
100  webSettings.setJavaScriptEnabled(true);
101  webSettings.setDatabaseEnabled(true);
102  webSettings.setDomStorageEnabled(true);
103  String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
104  webSettings.setDatabasePath(databasePath);
105 
106  // webview.setWebViewClient(new WebViewClient() {
107  // @Override
108  // public boolean shouldOverrideUrlLoading(WebView view, String url) {
109  // Log.i("WebView", "Handling url " + url);
110  // Uri uri = Uri.parse(url);
111  // String scheme = uri.getScheme();
112  // if (scheme.equals(Form.APPINVENTOR_URL_SCHEME)) {
113  // Intent resultIntent = new Intent();
114  // resultIntent.setData(uri);
115  // setResult(RESULT_OK, resultIntent);
116  // finish();
117  // } else {
118  // view.loadUrl(url);
119  // }
120  // return true;
121  // }
122  // });
123 
124  webview.setWebChromeClient(new WebChromeClient() {
125  @Override
126  public void onExceededDatabaseQuota(String url, String databaseIdentifier,
127  long currentQuota, long estimatedSize, long totalUsedQuota,
128  WebStorage.QuotaUpdater quotaUpdater) {
129  quotaUpdater.updateQuota(5 * 1024 * 1024);
130  }
131  });
132  setContentView(webview);
133  // Uncomment the line below to enable debugging
134  // the splash screen (splash.html)
135  //
136  // webview.setWebContentsDebuggingEnabled(true);
137  webview.addJavascriptInterface(android, "Android");
138  webview.loadUrl("file:///android_asset/splash.html");
139  }
140 
141  @Override
142  public void onRequestPermissionsResult(int code,
143  String permissions[], int[] grantResults) {
144  for (int i = 0; i < permissions.length; i++ ) {
145  String permission = permissions[i];
146  int grantResult = grantResults[i];
147  boolean granted = false;
148  if (grantResult == PackageManager.PERMISSION_GRANTED) {
149  granted = true;
150  }
151  webview.loadUrl("javascript:permresult('" + permission + "'," + granted + ")");
152  }
153  }
154 
155 }
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.SplashActivity.JavaInterface.JavaInterface
JavaInterface(Context context)
Definition: SplashActivity.java:48
com.google.appinventor.components.runtime.SplashActivity.JavaInterface.finished
void finished()
Definition: SplashActivity.java:82
com.google.appinventor.components
com.google.appinventor.components.runtime.SplashActivity
Definition: SplashActivity.java:40
com.google.appinventor.components.runtime.SplashActivity.onCreate
void onCreate(Bundle savedInstanceState)
Definition: SplashActivity.java:94
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.runtime.SplashActivity.JavaInterface.getVersion
String getVersion()
Definition: SplashActivity.java:71
com.google.appinventor.components.runtime.util.SdkLevel.LEVEL_MARSHMALLOW
static final int LEVEL_MARSHMALLOW
Definition: SdkLevel.java:36
com.google.appinventor.components.runtime.SplashActivity.JavaInterface.askPermission
void askPermission(String permission)
Definition: SplashActivity.java:65
com.google.appinventor.components.runtime.util.SdkLevel.getLevel
static int getLevel()
Definition: SdkLevel.java:45
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.SplashActivity.onRequestPermissionsResult
void onRequestPermissionsResult(int code, String permissions[], int[] grantResults)
Definition: SplashActivity.java:142
com.google.appinventor.components.runtime.AppInventorCompatActivity.setContentView
void setContentView(View view)
Definition: AppInventorCompatActivity.java:190
com.google.appinventor.components.runtime.AppInventorCompatActivity
Definition: AppInventorCompatActivity.java:40
com.google
com
com.google.appinventor.components.runtime.SplashActivity.JavaInterface.hasPermission
boolean hasPermission(String permission)
Definition: SplashActivity.java:53
com.google.appinventor.components.runtime.SplashActivity.JavaInterface
Definition: SplashActivity.java:45
com.google.appinventor