1 /* 2 * Copyright (C) 2018 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.dialer.app; 18 19 import android.annotation.TargetApi; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.os.Build; 25 import android.os.Build.VERSION_CODES; 26 import android.support.v4.content.pm.ShortcutInfoCompat; 27 import android.support.v4.content.pm.ShortcutManagerCompat; 28 import android.support.v4.graphics.drawable.IconCompat; 29 import com.android.dialer.configprovider.ConfigProviderBindings; 30 31 /** This class is a copy of dialer.main.impl.MainImpl to get around a dependency issue. */ 32 public class MainComponent { 33 34 private static final String SHORTCUT_KEY = "nui_launcher_shortcut"; 35 36 public static boolean isNewUiEnabled(Context context) { 37 return ConfigProviderBindings.get(context).getBoolean("is_nui_shortcut_enabled", false); 38 } 39 40 public static void createNewUiLauncherShortcut(Context context) { 41 enableComponent(context); 42 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 43 createLauncherShortcutO(context); 44 } else { 45 createLauncherShortcutPreO(context); 46 } 47 } 48 49 public static boolean isNuiComponentEnabled(Context context) { 50 if (!isNewUiEnabled(context)) { 51 return false; 52 } 53 return context 54 .getPackageManager() 55 .getComponentEnabledSetting(new ComponentName(context, getComponentName())) 56 == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 57 } 58 59 /** 60 * Enables the NUI activity component. By default the component is disabled and can't be accessed. 61 * Once the component has been enabled the user will get an option to use the new UI to handle 62 * DIAL (and other) intents. 63 */ 64 private static void enableComponent(Context context) { 65 context 66 .getPackageManager() 67 .setComponentEnabledSetting( 68 new ComponentName(context, getComponentName()), 69 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 70 PackageManager.DONT_KILL_APP); 71 } 72 73 @TargetApi(VERSION_CODES.O) 74 private static void createLauncherShortcutO(Context context) { 75 ShortcutInfoCompat shortcutInfo = 76 new ShortcutInfoCompat.Builder(context, SHORTCUT_KEY) 77 .setIcon(IconCompat.createWithResource(context, R.drawable.nui_launcher_icon)) 78 .setIntent(getIntent(context)) 79 .setShortLabel(context.getString(R.string.nui_shortcut_name)) 80 .build(); 81 ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null); 82 } 83 84 private static void createLauncherShortcutPreO(Context context) { 85 Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 86 intent.putExtra( 87 Intent.EXTRA_SHORTCUT_ICON, 88 Intent.ShortcutIconResource.fromContext(context, R.drawable.nui_launcher_icon)); 89 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.nui_shortcut_name)); 90 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getIntent(context)); 91 context.sendBroadcast(intent); 92 } 93 94 /** 95 * @param context Context of the application package implementing MainActivity class. 96 * @return intent for MainActivity.class 97 */ 98 public static Intent getIntent(Context context) { 99 Intent intent = new Intent(); 100 intent.setComponent(new ComponentName(context, getComponentName())); 101 intent.setAction(Intent.ACTION_VIEW); 102 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 103 return intent; 104 } 105 106 public static Intent getShowCallLogIntent(Context context) { 107 Intent intent = new Intent(); 108 intent.setComponent(new ComponentName(context, getComponentName())); 109 intent.setAction("ACTION_SHOW_TAB"); 110 intent.putExtra("EXTRA_SHOW_TAB", 1); 111 return intent; 112 } 113 114 public static Intent getShowVoicemailIntent(Context context) { 115 Intent intent = new Intent(); 116 intent.setComponent(new ComponentName(context, getComponentName())); 117 intent.setAction("ACTION_SHOW_TAB"); 118 intent.putExtra("EXTRA_SHOW_TAB", 3); 119 return intent; 120 } 121 122 private static String getComponentName() { 123 return "com.android.dialer.main.impl.MainActivity"; 124 } 125 } 126