|
1 | | -/* |
2 | | -* Copyright 2013 The Android Open Source Project |
3 | | -* |
4 | | -* Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | -* you may not use this file except in compliance with the License. |
6 | | -* You may obtain a copy of the License at |
7 | | -* |
8 | | -* http://www.apache.org/licenses/LICENSE-2.0 |
9 | | -* |
10 | | -* Unless required by applicable law or agreed to in writing, software |
11 | | -* distributed under the License is distributed on an "AS IS" BASIS, |
12 | | -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | -* See the License for the specific language governing permissions and |
14 | | -* limitations under the License. |
15 | | -*/ |
16 | | - |
17 | | - |
18 | 1 | package com.example.android.permissionrequest; |
19 | 2 |
|
| 3 | +import android.Manifest; |
| 4 | +import android.content.pm.PackageManager; |
| 5 | +import android.os.Build; |
20 | 6 | import android.os.Bundle; |
21 | | -import android.support.v4.app.FragmentTransaction; |
22 | | - |
23 | | -import com.example.android.common.activities.SampleActivityBase; |
| 7 | +import android.webkit.PermissionRequest; |
| 8 | +import android.webkit.WebChromeClient; |
| 9 | +import android.webkit.WebView; |
| 10 | +import androidx.appcompat.app.AppCompatActivity; |
| 11 | +import androidx.core.app.ActivityCompat; |
| 12 | +import androidx.core.content.ContextCompat; |
24 | 13 |
|
25 | | -/** |
26 | | - * A simple launcher activity containing a summary sample description, sample log and a custom |
27 | | - * {@link android.support.v4.app.Fragment} which can display a view. |
28 | | - * <p> |
29 | | - * For devices with displays with a width of 720dp or greater, the sample log is always visible, |
30 | | - * on other devices it's visibility is controlled by an item on the Action Bar. |
31 | | - */ |
32 | | -public class MainActivity extends SampleActivityBase { |
| 14 | +public class MainActivity extends AppCompatActivity { |
33 | 15 |
|
34 | | - public static final String TAG = "MainActivity"; |
| 16 | + private static final int PERMISSION_REQUEST_CODE = 1; |
| 17 | + private WebView webView; |
35 | 18 |
|
36 | 19 | @Override |
37 | 20 | protected void onCreate(Bundle savedInstanceState) { |
38 | 21 | super.onCreate(savedInstanceState); |
39 | | - setContentView(R.layout.activity_main); |
40 | 22 |
|
41 | | - if (savedInstanceState == null) { |
42 | | - FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); |
43 | | - PermissionRequestFragment fragment = new PermissionRequestFragment(); |
44 | | - transaction.replace(R.id.sample_content_fragment, fragment); |
45 | | - transaction.commit(); |
| 23 | + webView = new WebView(this); |
| 24 | + setContentView(webView); |
| 25 | + |
| 26 | + // Request microphone & camera permissions at runtime |
| 27 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 28 | + if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED || |
| 29 | + ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { |
| 30 | + ActivityCompat.requestPermissions(this, |
| 31 | + new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA}, |
| 32 | + PERMISSION_REQUEST_CODE); |
| 33 | + } |
46 | 34 | } |
47 | | - } |
48 | 35 |
|
| 36 | + // Enable mic & camera for WebView |
| 37 | + webView.getSettings().setJavaScriptEnabled(true); |
| 38 | + webView.getSettings().setMediaPlaybackRequiresUserGesture(false); |
| 39 | + webView.setWebChromeClient(new WebChromeClient() { |
| 40 | + @Override |
| 41 | + public void onPermissionRequest(final PermissionRequest request) { |
| 42 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 43 | + request.grant(request.getResources()); |
| 44 | + } |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + // Load your website |
| 49 | + webView.loadUrl("https://aidashboardartstudio.netlify.app/"); |
| 50 | + } |
49 | 51 | } |
0 commit comments