Home | History | Annotate | Download | only in am
      1 /*
      2  * Copyright (C) 2016 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 android.server.am;
     18 
     19 import static android.server.am.Components.BottomActivity.EXTRA_BOTTOM_WALLPAPER;
     20 import static android.server.am.Components.BottomActivity.EXTRA_STOP_DELAY;
     21 
     22 import android.os.Bundle;
     23 import android.os.SystemClock;
     24 import android.util.Log;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.WindowManager;
     28 
     29 public class BottomActivity extends AbstractLifecycleLogActivity {
     30 
     31     private int mStopDelay;
     32     private View mFloatingWindow;
     33 
     34     @Override
     35     protected void onCreate(Bundle savedInstanceState) {
     36         super.onCreate(savedInstanceState);
     37 
     38         final boolean useWallpaper = getIntent().getBooleanExtra(EXTRA_BOTTOM_WALLPAPER, false);
     39         if (useWallpaper) {
     40             setTheme(R.style.WallpaperTheme);
     41         }
     42         setContentView(R.layout.main);
     43 
     44         // Delayed stop is for simulating a case where resume happens before
     45         // activityStopped() is received by AM, and the transition starts without
     46         // going through fully stopped state (see b/30255354).
     47         // If enabled, we stall onStop() of BottomActivity, open TopActivity but make
     48         // it finish before onStop() ends. This will cause BottomActivity to resume before
     49         // it notifies AM of activityStopped(). We also add a second window of
     50         // TYPE_BASE_APPLICATION, so that the transition animation could start earlier.
     51         // Otherwise the main window has to relayout to visible first and the error won't occur.
     52         // Note that if the test fails, we shouldn't try to change the app here to make
     53         // it pass. The test app is artificially made to simulate an failure case, but
     54         // it's not doing anything wrong.
     55         mStopDelay = getIntent().getIntExtra(EXTRA_STOP_DELAY, 0);
     56         if (mStopDelay > 0) {
     57             LayoutInflater inflater = getLayoutInflater();
     58             mFloatingWindow = inflater.inflate(R.layout.floating, null);
     59 
     60             WindowManager.LayoutParams params = new WindowManager.LayoutParams();
     61             params.height = WindowManager.LayoutParams.WRAP_CONTENT;
     62             params.width = WindowManager.LayoutParams.WRAP_CONTENT;
     63             params.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
     64             params.setTitle("Floating");
     65             getWindowManager().addView(mFloatingWindow, params);
     66         }
     67     }
     68 
     69     @Override
     70     public void onResume() {
     71         Log.d(getTag(), "onResume() E");
     72         super.onResume();
     73 
     74         if (mStopDelay > 0) {
     75             // Refresh floating window
     76             Log.d(getTag(), "Scheduling invalidate Floating Window in onResume()");
     77             mFloatingWindow.invalidate();
     78         }
     79 
     80         Log.d(getTag(), "onResume() X");
     81     }
     82 
     83     @Override
     84     protected void onStop() {
     85         super.onStop();
     86 
     87         if (mStopDelay > 0) {
     88             Log.d(getTag(), "Stalling onStop() by " + mStopDelay + " ms...");
     89             SystemClock.sleep(mStopDelay);
     90 
     91             // Refresh floating window
     92             Log.d(getTag(), "Scheduling invalidate Floating Window in onStop()");
     93             mFloatingWindow.invalidate();
     94         }
     95     }
     96 }
     97