2525import android .appwidget .AppWidgetHostView ;
2626import android .appwidget .AppWidgetManager ;
2727import android .appwidget .AppWidgetProviderInfo ;
28+ import android .content .ComponentName ;
2829import android .content .Context ;
2930import android .content .Intent ;
3031import android .content .IntentSender ;
32+ import android .content .pm .ActivityInfo ;
33+ import android .content .pm .PackageManager ;
3134import android .content .pm .UserInfo ;
3235import android .content .res .Resources ;
3336import android .graphics .Canvas ;
3437import android .graphics .Rect ;
38+ import android .os .Bundle ;
3539import android .os .Looper ;
3640import android .os .Parcel ;
3741import android .os .Parcelable ;
3842import android .os .UserHandle ;
3943import android .os .UserManager ;
40- import android .provider .Settings ;
4144import android .util .AttributeSet ;
4245import android .util .Log ;
4346import android .util .Slog ;
5457import com .android .internal .widget .LockPatternUtils ;
5558
5659import java .io .File ;
60+ import java .util .ArrayList ;
5761import java .util .List ;
5862
5963public class KeyguardHostView extends KeyguardViewBase {
@@ -880,14 +884,22 @@ public void onClick(View v) {
880884
881885 @ Override
882886 public void run () {
883- Intent intent = new Intent (Settings .ACTION_SECURITY_SETTINGS );
884- intent .addFlags (
885- Intent .FLAG_ACTIVITY_NEW_TASK
886- | Intent .FLAG_ACTIVITY_SINGLE_TOP
887- | Intent .FLAG_ACTIVITY_CLEAR_TOP
888- | Intent .FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS );
889- mContext .startActivityAsUser (intent ,
890- new UserHandle (UserHandle .USER_CURRENT ));
887+ int defaultIconId = 0 ;
888+ Resources res = KeyguardHostView .this .getContext ().getResources ();
889+ ComponentName clock = new ComponentName (
890+ res .getString (R .string .widget_default_package_name ),
891+ res .getString (R .string .widget_default_class_name ));
892+ try {
893+ ActivityInfo activityInfo =
894+ mContext .getPackageManager ().getActivityInfo (clock , 0 );
895+ if (activityInfo != null ) {
896+ defaultIconId = activityInfo .icon ;
897+ }
898+ } catch (PackageManager .NameNotFoundException e ) {
899+ defaultIconId = 0 ;
900+ }
901+ launchPickActivityIntent (R .string .widget_default , defaultIconId , clock ,
902+ LockPatternUtils .EXTRA_DEFAULT_WIDGET );
891903 }
892904 });
893905 mCallback .dismiss (false );
@@ -898,6 +910,58 @@ public void run() {
898910 initializeTransportControl ();
899911 }
900912
913+ private void launchPickActivityIntent (int defaultLabelId , int defaultIconId ,
914+ ComponentName defaultComponentName , String defaultTag ) {
915+ // Create intent to pick widget
916+ Intent pickIntent = new Intent (AppWidgetManager .ACTION_KEYGUARD_APPWIDGET_PICK );
917+
918+ int appWidgetId = mAppWidgetHost .allocateAppWidgetId ();
919+ if (appWidgetId != -1 ) {
920+ pickIntent .putExtra (AppWidgetManager .EXTRA_APPWIDGET_ID , appWidgetId );
921+ pickIntent .putExtra (AppWidgetManager .EXTRA_CUSTOM_SORT , false );
922+ pickIntent .putExtra (AppWidgetManager .EXTRA_CATEGORY_FILTER ,
923+ AppWidgetProviderInfo .WIDGET_CATEGORY_KEYGUARD );
924+
925+ // Add an custom entry for the default
926+ AppWidgetProviderInfo defaultInfo = new AppWidgetProviderInfo ();
927+ ArrayList <AppWidgetProviderInfo > extraInfos = new ArrayList <AppWidgetProviderInfo >();
928+ defaultInfo .label = getResources ().getString (defaultLabelId );
929+ defaultInfo .icon = defaultIconId ;
930+ defaultInfo .provider = defaultComponentName ;
931+ extraInfos .add (defaultInfo );
932+
933+ ArrayList <Bundle > extraExtras = new ArrayList <Bundle >();
934+ Bundle b = new Bundle ();
935+ b .putBoolean (defaultTag , true );
936+ extraExtras .add (b );
937+
938+ // Launch the widget picker
939+ pickIntent .putExtra (AppWidgetManager .EXTRA_CUSTOM_INFO , extraInfos );
940+ pickIntent .putExtra (AppWidgetManager .EXTRA_CUSTOM_EXTRAS , extraExtras );
941+ pickIntent .putExtra (Intent .EXTRA_INTENT , getBaseIntent ());
942+ pickIntent .addFlags (
943+ Intent .FLAG_ACTIVITY_NEW_TASK
944+ | Intent .FLAG_ACTIVITY_SINGLE_TOP
945+ | Intent .FLAG_ACTIVITY_CLEAR_TOP
946+ | Intent .FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS );
947+ mContext .startActivityAsUser (pickIntent ,
948+ new UserHandle (UserHandle .USER_CURRENT ));
949+ } else {
950+ Log .e (TAG , "Unable to allocate an AppWidget id in lock screen" );
951+ }
952+ }
953+
954+ private Intent getBaseIntent () {
955+ Intent baseIntent = new Intent (Intent .ACTION_MAIN , null );
956+ baseIntent .addCategory (Intent .CATEGORY_DEFAULT );
957+
958+ Bundle options = new Bundle ();
959+ options .putInt (AppWidgetManager .OPTION_APPWIDGET_HOST_CATEGORY ,
960+ AppWidgetProviderInfo .WIDGET_CATEGORY_KEYGUARD );
961+ baseIntent .putExtra (AppWidgetManager .EXTRA_APPWIDGET_OPTIONS , options );
962+ return baseIntent ;
963+ }
964+
901965 private void removeTransportFromWidgetPager () {
902966 int page = getWidgetPosition (R .id .keyguard_transport_control );
903967 if (page != -1 ) {
0 commit comments