1 /* 2 * Copyright 2014, 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.managedprovisioning.common; 18 19 import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; 20 21 import static com.android.internal.logging.nano.MetricsProto.MetricsEvent.VIEW_UNKNOWN; 22 23 import android.app.Activity; 24 import android.app.ActivityManager.TaskDescription; 25 import android.app.DialogFragment; 26 import android.app.Fragment; 27 import android.app.FragmentManager; 28 import android.app.FragmentTransaction; 29 import android.content.pm.ActivityInfo; 30 import android.graphics.Color; 31 import android.os.Bundle; 32 import android.support.annotation.VisibleForTesting; 33 import android.view.View; 34 import android.view.Window; 35 import android.view.WindowManager; 36 37 import com.android.managedprovisioning.R; 38 import com.android.managedprovisioning.analytics.TimeLogger; 39 40 /** 41 * Base class for setting up the layout. 42 */ 43 public abstract class SetupLayoutActivity extends Activity { 44 protected final Utils mUtils; 45 46 private TimeLogger mTimeLogger; 47 SetupLayoutActivity()48 public SetupLayoutActivity() { 49 this(new Utils()); 50 } 51 52 @VisibleForTesting SetupLayoutActivity(Utils utils)53 protected SetupLayoutActivity(Utils utils) { 54 mUtils = utils; 55 } 56 57 @Override onCreate(Bundle savedInstanceState)58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 mTimeLogger = new TimeLogger(this, getMetricsCategory()); 61 mTimeLogger.start(); 62 63 // lock orientation to portrait on phones 64 if (getResources().getBoolean(R.bool.lock_to_portrait)) { 65 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 66 } 67 } 68 69 @Override onDestroy()70 public void onDestroy() { 71 mTimeLogger.stop(); 72 super.onDestroy(); 73 } 74 getMetricsCategory()75 protected int getMetricsCategory() { 76 return VIEW_UNKNOWN; 77 } 78 getUtils()79 protected Utils getUtils() { 80 return mUtils; 81 } 82 83 /** 84 * @param mainColor integer representing the color (i.e. not resource id) 85 */ setMainColor(int mainColor)86 protected void setMainColor(int mainColor) { 87 mainColor = toSolidColor(mainColor); 88 89 Window window = getWindow(); 90 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 91 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 92 window.setStatusBarColor(mainColor); 93 94 // set status bar icon style 95 View decorView = getWindow().getDecorView(); 96 int visibility = decorView.getSystemUiVisibility(); 97 decorView.setSystemUiVisibility(getUtils().isBrightColor(mainColor) 98 ? (visibility | SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) 99 : (visibility & ~SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)); 100 101 setTaskDescription(new TaskDescription(null /* label */, null /* icon */, mainColor)); 102 } 103 104 /** 105 * Removes transparency from the color 106 * 107 * <p>Needed for correct calculation of Status Bar icons (light / dark) 108 */ toSolidColor(Integer color)109 private Integer toSolidColor(Integer color) { 110 return Color.argb(255, Color.red(color), Color.green(color), Color.blue(color)); 111 } 112 113 /** 114 * Constructs and shows a {@link DialogFragment} unless it is already displayed. 115 * @param dialogBuilder Lightweight builder, that it is inexpensive to discard it if dialog 116 * already shown. 117 * @param tag The tag for this dialog, as per {@link FragmentTransaction#add(Fragment, String)}. 118 */ showDialog(DialogBuilder dialogBuilder, String tag)119 protected void showDialog(DialogBuilder dialogBuilder, String tag) { 120 FragmentManager fragmentManager = getFragmentManager(); 121 if (!isDialogAdded(tag)) { 122 dialogBuilder.build().show(fragmentManager, tag); 123 } 124 } 125 126 /** 127 * Checks whether the {@link DialogFragment} associated with the given tag is currently showing. 128 * @param tag The tag for this dialog. 129 */ isDialogAdded(String tag)130 protected boolean isDialogAdded(String tag) { 131 Fragment fragment = getFragmentManager().findFragmentByTag(tag); 132 return (fragment != null) && (fragment.isAdded()); 133 } 134 }