• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.android.car.rotaryplayground;
18 
19 import android.content.res.Resources;
20 import android.os.Bundle;
21 import android.util.Base64;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.webkit.WebView;
27 import android.widget.Button;
28 import android.widget.CheckBox;
29 
30 import androidx.annotation.Nullable;
31 import androidx.fragment.app.Fragment;
32 
33 import java.io.IOException;
34 import java.io.InputStream;
35 
36 /** Fragment to demo a layout with a {@link WebView}. */
37 public class WebViewFragment extends Fragment {
38 
39     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)40     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
41             @Nullable Bundle savedInstanceState) {
42         View view = inflater.inflate(R.layout.rotary_web_view, container, false);
43         Button toggleButtonsButton = view.findViewById(R.id.toggle_buttons);
44         Button topButton = view.findViewById(R.id.top_button);
45         WebView webView = view.findViewById(R.id.web_view);
46         Button bottomButton = view.findViewById(R.id.bottom_button);
47 
48         toggleButtonsButton.setOnClickListener(v -> {
49             int buttonVisibility =
50                     topButton.getVisibility() == View.GONE ? View.VISIBLE : View.GONE;
51             topButton.setVisibility(buttonVisibility);
52             bottomButton.setVisibility(buttonVisibility);
53         });
54 
55         Resources res = getResources();
56         InputStream inputStream = res.openRawResource(R.raw.web_view_html);
57         byte[] byteArray = new byte[0];
58         try {
59             byteArray = new byte[inputStream.available()];
60             inputStream.read(byteArray);
61         } catch (IOException e) {
62             Log.w("WebViewFragment", "Can't read HTML");
63         }
64         String webViewHtml = new String(byteArray);
65         String encodedHtml = Base64.encodeToString(webViewHtml.getBytes(), Base64.NO_PADDING);
66         webView.loadData(encodedHtml, "text/html", "base64");
67         return view;
68     }
69 }
70