1 /*
2  * Copyright (C) 2018 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.google.android.car.kitchensink.weblinks;
18 
19 import android.content.Intent;
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.LinearLayout;
26 
27 import androidx.annotation.Nullable;
28 import androidx.fragment.app.Fragment;
29 
30 import com.google.android.car.kitchensink.R;
31 
32 /**
33  * This fragment just has a few links to web pages.
34  */
35 public class WebLinksTestFragment extends Fragment {
36     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)37     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
38             @Nullable Bundle savedInstanceState) {
39         View root = inflater.inflate(R.layout.weblinks_fragment, container, false);
40 
41         LinearLayout buttons = root.findViewById(R.id.buttons);
42         for (int i = 0; i < buttons.getChildCount(); i++) {
43             buttons.getChildAt(i).setOnClickListener(this::onClick);
44         }
45 
46         return root;
47     }
48 
onClick(View view)49     private void onClick(View view) {
50         String url = view.getTag().toString();
51 
52         if (!url.startsWith("http")) {
53             url = "http://" + url;
54         }
55         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
56     }
57 }
58