Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
sail | 3bcea98 | 2017-09-03 13:57:22 -0700 | [diff] [blame] | 17 | package com.android.bubble; |
Eric Erfanian | 2ca4318 | 2017-08-31 06:57:16 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Configuration; |
| 21 | import android.support.annotation.NonNull; |
| 22 | import android.view.KeyEvent; |
| 23 | import android.widget.FrameLayout; |
| 24 | |
| 25 | /** |
| 26 | * ViewGroup that handles some overlay window concerns. Allows back button and configuration change |
| 27 | * events to be listened for via interfaces. |
| 28 | */ |
| 29 | public class WindowRoot extends FrameLayout { |
| 30 | |
| 31 | /** Callback for when the back button is pressed while this window is in focus */ |
| 32 | public interface OnBackPressedListener { |
| 33 | boolean onBackPressed(); |
| 34 | } |
| 35 | |
| 36 | /** Callback for when the Configuration changes for this window */ |
| 37 | public interface OnConfigurationChangedListener { |
| 38 | void onConfigurationChanged(Configuration newConfiguration); |
| 39 | } |
| 40 | |
| 41 | private OnBackPressedListener backPressedListener; |
| 42 | private OnConfigurationChangedListener configurationChangedListener; |
| 43 | |
| 44 | public WindowRoot(@NonNull Context context) { |
| 45 | super(context); |
| 46 | } |
| 47 | |
| 48 | public void setOnBackPressedListener(OnBackPressedListener listener) { |
| 49 | backPressedListener = listener; |
| 50 | } |
| 51 | |
| 52 | public void setOnConfigurationChangedListener(OnConfigurationChangedListener listener) { |
| 53 | configurationChangedListener = listener; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 58 | if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && backPressedListener != null) { |
| 59 | if (event.getAction() == KeyEvent.ACTION_UP) { |
| 60 | return backPressedListener.onBackPressed(); |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | return super.dispatchKeyEvent(event); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 69 | super.dispatchConfigurationChanged(newConfig); |
| 70 | if (configurationChangedListener != null) { |
| 71 | configurationChangedListener.onConfigurationChanged(newConfig); |
| 72 | } |
| 73 | } |
| 74 | } |