1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package android.app.cts; 17 18 import android.app.Activity; 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.os.Bundle; 22 import android.view.WindowManager; 23 24 import java.util.concurrent.CompletableFuture; 25 import java.util.concurrent.Future; 26 27 public class ApplyOverrideConfigurationActivity extends Activity { 28 public static final int OVERRIDE_SMALLEST_WIDTH = 99999; 29 30 private CompletableFuture<Configuration> mOnConfigurationChangedFuture = null; 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 36 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD 37 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON 38 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 39 } 40 41 @Override 42 protected void attachBaseContext(Context newBase) { 43 super.attachBaseContext(newBase); 44 45 final Configuration overrideConfig = new Configuration(); 46 overrideConfig.smallestScreenWidthDp = OVERRIDE_SMALLEST_WIDTH; 47 applyOverrideConfiguration(overrideConfig); 48 } 49 50 @Override 51 public void onConfigurationChanged(Configuration newConfig) { 52 super.onConfigurationChanged(newConfig); 53 54 synchronized (this) { 55 if (mOnConfigurationChangedFuture != null) { 56 mOnConfigurationChangedFuture.complete(new Configuration(newConfig)); 57 mOnConfigurationChangedFuture = null; 58 } 59 } 60 } 61 62 /** 63 * Hands back a Future that will be completed when onConfigurationChanged() is called. 64 * It will only report a single call to onConfigurationChanged(). Subsequent calls can be 65 * captured by calling this method again. Calling this method will cancel all past 66 * Futures received from this method. 67 * @return A Future that completes with the configuration passed in to onConfigurationChanged(). 68 */ 69 public synchronized Future<Configuration> watchForSingleOnConfigurationChangedCallback() { 70 if (mOnConfigurationChangedFuture != null) { 71 mOnConfigurationChangedFuture.cancel(true); 72 } 73 74 mOnConfigurationChangedFuture = new CompletableFuture<>(); 75 return mOnConfigurationChangedFuture; 76 } 77 } 78