Home | History | Annotate | Download | only in intentplayground
      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.example.android.intentplayground;
     18 
     19 import android.app.Activity;
     20 
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.ArrayAdapter;
     27 import android.widget.LinearLayout;
     28 import android.widget.TextView;
     29 import androidx.annotation.Nullable;
     30 import androidx.fragment.app.Fragment;
     31 import java.util.List;
     32 import java.util.Set;
     33 
     34 /**
     35  * Displays details about the intent that launched the current activity.
     36  */
     37 public class IntentFragment extends Fragment {
     38     private TextView mActionTextView, mUriTextView, mTypeTextView, mPackageTextView;
     39     private LinearLayout mCategoryListLayout, mFlagListLayout;
     40 
     41     @Nullable
     42     @Override
     43     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
     44                              Bundle savedInstanceState) {
     45         LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_intent, container,
     46                 false /* attachToRoot */);
     47         // Get handles to views
     48         mActionTextView = layout.findViewById(R.id.intentAction);
     49         mUriTextView = layout.findViewById(R.id.intentUri);
     50         mTypeTextView = layout.findViewById(R.id.intentType);
     51         mPackageTextView = layout.findViewById(R.id.intentPackage);
     52         mCategoryListLayout = layout.findViewById(R.id.intentCategories);
     53         mFlagListLayout = layout.findViewById(R.id.intentFlags);
     54         return layout;
     55     }
     56 
     57     @Override
     58     public void onResume() {
     59         super.onResume();
     60         Activity activity = getActivity();
     61         Intent intent = activity.getIntent();
     62         // Get intent info
     63         String action = intent.getAction();
     64         String dataUri = intent.getDataString();
     65         String intentType = intent.getType();
     66         String intentPackage = intent.getPackage();
     67         Set<String> categories = intent.getCategories();
     68         List<String> flags = FlagUtils.discoverFlags(intent);
     69         // set data
     70         mActionTextView.setText(action);
     71         mUriTextView.setText(dataUri);
     72         mTypeTextView.setText(intentType);
     73         mPackageTextView.setText(intentPackage);
     74         if (categories != null) {
     75             ArrayAdapter<String> categoryAdapter = new ArrayAdapter<>(activity,
     76                     R.layout.simple_list_item,
     77                     categories.toArray(new String[0]));
     78             fillLayout(mCategoryListLayout, categoryAdapter);
     79         }
     80         ArrayAdapter<String> flagAdapter = new ArrayAdapter<>(activity,
     81                 R.layout.simple_list_item, flags);
     82         fillLayout(mFlagListLayout, flagAdapter);
     83     }
     84 
     85     /**
     86      * Takes a @{link ViewGroup} and uses the given adapter to fill it; used in the cases where we
     87      * need a non-scrollable list that is a child of {@link android.widget.ScrollView}.
     88      * @param layout The layout to be filled.
     89      * @param adapter The adapter that provides the views for the layout.
     90      */
     91     private void fillLayout(ViewGroup layout, ArrayAdapter<?> adapter) {
     92         layout.removeAllViews();
     93         for (int i = 0; i < adapter.getCount(); i++) {
     94             layout.addView(adapter.getView(i, null /* convertView */, null /* parent */));
     95         }
     96     }
     97 }
     98