Home | History | Annotate | Download | only in statusbar
      1 /*
      2  * Copyright (C) 2019 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.systemui.statusbar;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.service.notification.StatusBarNotification;
     22 import android.util.FeatureFlagUtils;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.view.ViewParent;
     26 
     27 import com.android.settingslib.media.MediaOutputSliceConstants;
     28 import com.android.systemui.Dependency;
     29 import com.android.systemui.plugins.ActivityStarter;
     30 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
     31 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
     32 
     33 /**
     34  * Class for handling MediaTransfer state over a set of notifications.
     35  */
     36 public class MediaTransferManager {
     37     private final Context mContext;
     38     private final ActivityStarter mActivityStarter;
     39 
     40     private final View.OnClickListener mOnClickHandler = new View.OnClickListener() {
     41         @Override
     42         public void onClick(View view) {
     43             if (handleMediaTransfer(view)) {
     44                 return;
     45             }
     46         }
     47 
     48         private boolean handleMediaTransfer(View view) {
     49             if (view.findViewById(com.android.internal.R.id.media_seamless) == null) {
     50                 return false;
     51             }
     52 
     53             ViewParent parent = view.getParent();
     54             StatusBarNotification statusBarNotification = getNotificationForParent(parent);
     55             final Intent intent = new Intent()
     56                     .setAction(MediaOutputSliceConstants.ACTION_MEDIA_OUTPUT)
     57                     .putExtra(MediaOutputSliceConstants.EXTRA_PACKAGE_NAME,
     58                             statusBarNotification.getPackageName());
     59             mActivityStarter.startActivity(intent, false, true /* dismissShade */,
     60                     Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
     61             return true;
     62         }
     63 
     64         private StatusBarNotification getNotificationForParent(ViewParent parent) {
     65             while (parent != null) {
     66                 if (parent instanceof ExpandableNotificationRow) {
     67                     return ((ExpandableNotificationRow) parent).getStatusBarNotification();
     68                 }
     69                 parent = parent.getParent();
     70             }
     71             return null;
     72         }
     73     };
     74 
     75     public MediaTransferManager(Context context) {
     76         mContext = context;
     77         mActivityStarter = Dependency.get(ActivityStarter.class);
     78     }
     79 
     80     /**
     81      * apply the action button for MediaTransfer
     82      *
     83      * @param root  The parent container of the view.
     84      * @param entry The entry of MediaTransfer action button.
     85      */
     86     public void applyMediaTransferView(ViewGroup root, NotificationEntry entry) {
     87         if (!FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SEAMLESS_TRANSFER)) {
     88             return;
     89         }
     90 
     91         View view = root.findViewById(com.android.internal.R.id.media_seamless);
     92         if (view == null) {
     93             return;
     94         }
     95 
     96         view.setVisibility(View.VISIBLE);
     97         view.setOnClickListener(mOnClickHandler);
     98     }
     99 }
    100