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 17 package com.android.dialer.main.impl; 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 import com.android.dialer.main.Main; 31 import javax.inject.Inject; 32 33 /** The entry point for the main feature. */ 34 final class MainImpl implements Main { 35 private static final String SHORTCUT_KEY = "nui_launcher_shortcut"; 36 37 @Inject MainImpl()38 MainImpl() {} 39 40 @Override isNewUiEnabled(Context context)41 public boolean isNewUiEnabled(Context context) { 42 return ConfigProviderBindings.get(context).getBoolean("is_nui_shortcut_enabled", false); 43 } 44 45 @Override createNewUiLauncherShortcut(Context context)46 public void createNewUiLauncherShortcut(Context context) { 47 enableComponent(context); 48 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 49 createLauncherShortcutO(context); 50 } else { 51 createLauncherShortcutPreO(context); 52 } 53 } 54 55 /** 56 * Enables the NUI activity component. By default the component is disabled and can't be accessed. 57 * Once the component has been enabled the user will get an option to use the new UI to handle 58 * DIAL (and other) intents. 59 */ enableComponent(Context context)60 private static void enableComponent(Context context) { 61 context 62 .getPackageManager() 63 .setComponentEnabledSetting( 64 new ComponentName(context, MainActivity.class), 65 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 66 PackageManager.DONT_KILL_APP); 67 } 68 69 @Override disableComponentForTesting(Context context)70 public void disableComponentForTesting(Context context) { 71 context 72 .getPackageManager() 73 .setComponentEnabledSetting( 74 new ComponentName(context, MainActivity.class), 75 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 76 PackageManager.DONT_KILL_APP); 77 } 78 79 @TargetApi(VERSION_CODES.O) createLauncherShortcutO(Context context)80 private static void createLauncherShortcutO(Context context) { 81 ShortcutInfoCompat shortcutInfo = 82 new ShortcutInfoCompat.Builder(context, SHORTCUT_KEY) 83 .setIcon(IconCompat.createWithResource(context, R.drawable.nui_launcher_icon)) 84 .setIntent(MainActivity.getIntent(context)) 85 .setShortLabel(context.getString(R.string.nui_shortcut_name)) 86 .build(); 87 ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null); 88 } 89 createLauncherShortcutPreO(Context context)90 private static void createLauncherShortcutPreO(Context context) { 91 Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 92 intent.putExtra( 93 Intent.EXTRA_SHORTCUT_ICON, 94 Intent.ShortcutIconResource.fromContext(context, R.drawable.nui_launcher_icon)); 95 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.nui_shortcut_name)); 96 intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, MainActivity.getIntent(context)); 97 context.sendBroadcast(intent); 98 } 99 } 100