Home | History | Annotate | Download | only in browseractions
      1 /*
      2  * Copyright 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 androidx.browser.browseractions;
     18 
     19 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
     20 
     21 import android.app.PendingIntent;
     22 import android.app.PendingIntent.CanceledException;
     23 import android.content.Context;
     24 import android.content.DialogInterface;
     25 import android.net.Uri;
     26 import android.text.TextUtils;
     27 import android.util.Log;
     28 import android.view.LayoutInflater;
     29 import android.view.View;
     30 import android.widget.AdapterView;
     31 import android.widget.ListView;
     32 import android.widget.TextView;
     33 
     34 import androidx.annotation.RestrictTo;
     35 import androidx.annotation.VisibleForTesting;
     36 import androidx.browser.R;
     37 import androidx.core.widget.TextViewCompat;
     38 
     39 import java.util.List;
     40 
     41 /**
     42  * The class to show fallback menu for Browser Actions if no provider is available.
     43  */
     44 class BrowserActionsFallbackMenuUi implements AdapterView.OnItemClickListener {
     45     /** @hide */
     46     @VisibleForTesting
     47     @RestrictTo(LIBRARY_GROUP)
     48     interface BrowserActionsFallMenuUiListener {
     49         void onMenuShown(View view);
     50     }
     51 
     52     private static final String TAG = "BrowserActionskMenuUi";
     53 
     54     private final Context mContext;
     55     private final Uri mUri;
     56     private final List<BrowserActionItem> mMenuItems;
     57 
     58     private BrowserActionsFallMenuUiListener mMenuUiListener;
     59 
     60     private BrowserActionsFallbackMenuDialog mBrowserActionsDialog;
     61 
     62     /**
     63      * @param context The {@link Context} used to show the fallback menu.
     64      * @param uri The uri which users click to trigger the menu.
     65      * @param menuItems The custom menu items shown in the menu.
     66      */
     67     BrowserActionsFallbackMenuUi(
     68             Context context, Uri uri, List<BrowserActionItem> menuItems) {
     69         mContext = context;
     70         mUri = uri;
     71         mMenuItems = menuItems;
     72     }
     73 
     74     /** @hide */
     75     @VisibleForTesting
     76     @RestrictTo(LIBRARY_GROUP)
     77     void setMenuUiListener(BrowserActionsFallMenuUiListener menuUiListener) {
     78         mMenuUiListener = menuUiListener;
     79     }
     80 
     81     /**
     82      * Shows the fallback menu.
     83      */
     84     public void displayMenu() {
     85         final View view = LayoutInflater.from(mContext).inflate(
     86                 R.layout.browser_actions_context_menu_page, null);
     87         mBrowserActionsDialog = new BrowserActionsFallbackMenuDialog(mContext, initMenuView(view));
     88         mBrowserActionsDialog.setContentView(view);
     89         if (mMenuUiListener != null) {
     90             mBrowserActionsDialog.setOnShowListener(new DialogInterface.OnShowListener() {
     91                 @Override
     92                 public void onShow(DialogInterface dialogInterface) {
     93                     mMenuUiListener.onMenuShown(view);
     94                 }
     95             });
     96         }
     97         mBrowserActionsDialog.show();
     98     }
     99 
    100     private BrowserActionsFallbackMenuView initMenuView(View view) {
    101         BrowserActionsFallbackMenuView menuView =
    102                 (BrowserActionsFallbackMenuView) view.findViewById(R.id.browser_actions_menu_view);
    103 
    104         final TextView urlTextView = (TextView) view.findViewById(R.id.browser_actions_header_text);
    105         urlTextView.setText(mUri.toString());
    106         urlTextView.setOnClickListener(new View.OnClickListener() {
    107             @Override
    108             public void onClick(View view) {
    109                 if (TextViewCompat.getMaxLines(urlTextView) == Integer.MAX_VALUE) {
    110                     urlTextView.setMaxLines(1);
    111                     urlTextView.setEllipsize(TextUtils.TruncateAt.END);
    112                 } else {
    113                     urlTextView.setMaxLines(Integer.MAX_VALUE);
    114                     urlTextView.setEllipsize(null);
    115                 }
    116             }
    117         });
    118 
    119         ListView menuListView = (ListView) view.findViewById(R.id.browser_actions_menu_items);
    120         BrowserActionsFallbackMenuAdapter adapter =
    121                 new BrowserActionsFallbackMenuAdapter(mMenuItems, mContext);
    122         menuListView.setAdapter(adapter);
    123         menuListView.setOnItemClickListener(this);
    124 
    125         return menuView;
    126     }
    127 
    128     @Override
    129     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    130         PendingIntent action = mMenuItems.get(position).getAction();
    131         try {
    132             action.send();
    133             mBrowserActionsDialog.dismiss();
    134         } catch (CanceledException e) {
    135             Log.e(TAG, "Failed to send custom item action", e);
    136         }
    137     }
    138 }
    139