1 /* 2 * Copyright 2022 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 package com.example.androidx.webkit; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.webkit.WebView; 23 import android.webkit.WebViewClient; 24 import android.widget.TextView; 25 26 import androidx.appcompat.app.AppCompatActivity; 27 import androidx.core.content.ContextCompat; 28 import androidx.webkit.ProcessGlobalConfig; 29 import androidx.webkit.WebViewFeature; 30 31 import org.jspecify.annotations.Nullable; 32 33 import java.io.File; 34 35 /** 36 * An {@link Activity} which makes use of 37 * {@link androidx.webkit.ProcessGlobalConfig#setDirectoryBasePaths(Context, File, File)} 38 */ 39 public class DirectoryBasePathsActivity extends AppCompatActivity { 40 @Override onCreate(@ullable Bundle savedInstanceState)41 protected void onCreate(@Nullable Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setTitle(R.string.directory_base_path_activity_title); 44 WebkitHelpers.appendWebViewVersionToTitle(this); 45 46 if (!WebViewFeature.isStartupFeatureSupported(this, 47 WebViewFeature.STARTUP_FEATURE_SET_DIRECTORY_BASE_PATHS)) { 48 WebkitHelpers.showMessageInActivity(this, R.string.webkit_api_not_available); 49 return; 50 } 51 52 File dataBasePath = 53 new File(ContextCompat.getDataDir(this), 54 "data_dir"); 55 File cacheBasePath = 56 new File(ContextCompat.getDataDir(this), 57 "cache_dir"); 58 ProcessGlobalConfig config = new ProcessGlobalConfig(); 59 config.setDirectoryBasePaths(this, dataBasePath, cacheBasePath); 60 config.setDataDirectorySuffix(this, "directory_base_path_activity_suffix"); 61 ProcessGlobalConfig.apply(config); 62 setContentView(R.layout.activity_data_directory_config); 63 WebView wv = findViewById(R.id.data_directory_config_webview); 64 wv.getSettings().setJavaScriptEnabled(true); 65 wv.setWebViewClient(new WebViewClient()); 66 wv.loadUrl("www.google.com"); 67 TextView tv = findViewById(R.id.data_directory_config_textview); 68 tv.setText("WebView Loaded!"); 69 } 70 } 71