19
19
import android .accounts .Account ;
20
20
import android .accounts .AccountManager ;
21
21
import android .app .Activity ;
22
- import android .app .admin .DevicePolicyManager ;
23
- import android .content .ComponentName ;
22
+ import android .content .BroadcastReceiver ;
24
23
import android .content .Context ;
24
+ import android .content .Intent ;
25
+ import android .content .IntentFilter ;
25
26
import android .content .pm .ApplicationInfo ;
26
27
import android .content .pm .PackageManager ;
27
28
import android .os .Bundle ;
29
+ import android .support .v4 .content .LocalBroadcastManager ;
28
30
import android .util .Log ;
29
31
import android .view .View ;
32
+ import android .widget .Button ;
30
33
import android .widget .ImageView ;
31
34
import android .widget .TextView ;
32
35
33
36
import com .afwsamples .testdpc .common .LaunchIntentUtil ;
37
+ import com .afwsamples .testdpc .provision .CheckInState ;
38
+ import com .afwsamples .testdpc .provision .ProvisioningUtil ;
34
39
import com .android .setupwizardlib .SetupWizardLayout ;
35
40
import com .android .setupwizardlib .view .NavigationBar ;
36
41
42
+ import static com .afwsamples .testdpc .provision .CheckInState .FIRST_ACCOUNT_READY_PROCESSED_ACTION ;
43
+
37
44
/**
38
45
* This activity is started after managed profile provisioning is complete in
39
- * {@link DeviceAdminReceiver}. It is responsible for enabling the managed profile and providing a
40
- * shortcut to the policy management screen.
46
+ * {@link DeviceAdminReceiver}. There could be two cases:
47
+ * 1. If we are not going to add account now, we will then enable profile immediately.
48
+ * 2. If we have just added account, we need to wait for the FIRST_ACCOUNT_READY broadcast before
49
+ * enabling the profile. The broadcast indicates that the account has been synced with Google
50
+ * and is ready for use.
41
51
*/
42
52
public class EnableProfileActivity extends Activity implements NavigationBar .NavigationBarListener {
53
+ private CheckInStateReceiver mCheckInStateReceiver ;
54
+ private Button mFinishButton ;
55
+ private SetupWizardLayout mSetupWizardLayout ;
56
+
57
+ private boolean mEnableProfileNow ;
58
+
59
+ public static final String EXTRA_ENABLE_PROFILE_NOW = "enable_profile_now" ;
60
+ private static final IntentFilter sIntentFilter =
61
+ new IntentFilter (FIRST_ACCOUNT_READY_PROCESSED_ACTION );
62
+ private static final long WAIT_FOR_FIRST_ACCOUNT_READY_TIMEOUT = 60 * 1000 ;
43
63
44
64
@ Override
45
65
protected void onCreate (Bundle savedInstanceState ) {
46
66
super .onCreate (savedInstanceState );
47
- // Don't enable the profile again if this activity is being re-initialized.
48
- if (null == savedInstanceState ) {
49
- // Important: After the profile has been created, the MDM must enable it for corporate
50
- // apps to become visible in the launcher.
51
- enableProfile ();
67
+ mEnableProfileNow = getIntent ().getBooleanExtra (EXTRA_ENABLE_PROFILE_NOW , false );
68
+ if (savedInstanceState == null ) {
69
+ if (mEnableProfileNow ) {
70
+ ProvisioningUtil .enableProfile (this );
71
+ } else {
72
+ // Set up an alarm to enable profile in case we do not receive first account ready
73
+ // broadcast for whatever reason.
74
+ FirstAccountReadyBroadcastReceiver .scheduleFirstAccountReadyTimeoutAlarm (
75
+ this , WAIT_FOR_FIRST_ACCOUNT_READY_TIMEOUT );
76
+ }
52
77
}
53
-
54
- // This is just a user friendly shortcut to the policy management screen of this app.
55
78
setContentView (R .layout .enable_profile_activity );
56
- SetupWizardLayout layout = (SetupWizardLayout ) findViewById (R .id .setup_wizard_layout );
57
- NavigationBar navigationBar = layout .getNavigationBar ();
79
+ mSetupWizardLayout = (SetupWizardLayout ) findViewById (R .id .setup_wizard_layout );
80
+ NavigationBar navigationBar = mSetupWizardLayout .getNavigationBar ();
81
+ navigationBar .getBackButton ().setEnabled (false );
58
82
navigationBar .setNavigationBarListener (this );
59
- navigationBar .getNextButton ().setText (R .string .finish_button );
83
+ mFinishButton = navigationBar .getNextButton ();
84
+ mFinishButton .setText (R .string .finish_button );
85
+
86
+ mCheckInStateReceiver = new CheckInStateReceiver ();
60
87
88
+ // This is just a user friendly shortcut to the policy management screen of this app.
61
89
ImageView appIcon = (ImageView ) findViewById (R .id .app_icon );
62
90
TextView appLabel = (TextView ) findViewById (R .id .app_label );
63
91
try {
@@ -86,6 +114,22 @@ protected void onCreate(Bundle savedInstanceState) {
86
114
}
87
115
}
88
116
117
+ @ Override
118
+ protected void onResume () {
119
+ super .onResume ();
120
+ LocalBroadcastManager .getInstance (this ).registerReceiver (mCheckInStateReceiver ,
121
+ sIntentFilter );
122
+ // In case the broadcast is sent before we register the receiver.
123
+ CheckInState checkInState = new CheckInState (this );
124
+ refreshUi (mEnableProfileNow || checkInState .isFirstAccountReady () /* enableFinish */ );
125
+ }
126
+
127
+ @ Override
128
+ protected void onStop () {
129
+ super .onStop ();
130
+ LocalBroadcastManager .getInstance (this ).unregisterReceiver (mCheckInStateReceiver );
131
+ }
132
+
89
133
private boolean isAccountMigrated (String addedAccount ) {
90
134
Account [] accounts = AccountManager .get (this ).getAccounts ();
91
135
for (Account account : accounts ) {
@@ -96,14 +140,17 @@ private boolean isAccountMigrated(String addedAccount) {
96
140
return false ;
97
141
}
98
142
99
- private void enableProfile () {
100
- DevicePolicyManager manager = (DevicePolicyManager ) getSystemService (
101
- Context .DEVICE_POLICY_SERVICE );
102
- ComponentName componentName = DeviceAdminReceiver .getComponentName (this );
103
- // This is the name for the newly created managed profile.
104
- manager .setProfileName (componentName , getString (R .string .profile_name ));
105
- // We enable the profile here.
106
- manager .setProfileEnabled (componentName );
143
+ private void refreshUi (boolean enableFinish ) {
144
+ if (enableFinish ) {
145
+ mSetupWizardLayout .hideProgressBar ();
146
+ } else {
147
+ mSetupWizardLayout .showProgressBar ();
148
+ }
149
+ mSetupWizardLayout .setHeaderText (
150
+ (enableFinish )
151
+ ? R .string .finish_setup
152
+ : R .string .waiting_for_first_account_check_in );
153
+ mFinishButton .setEnabled (enableFinish );
107
154
}
108
155
109
156
@ Override
@@ -115,4 +162,12 @@ public void onNavigateBack() {
115
162
public void onNavigateNext () {
116
163
finish ();
117
164
}
165
+
166
+ class CheckInStateReceiver extends BroadcastReceiver {
167
+ @ Override
168
+ public void onReceive (Context context , Intent intent ) {
169
+ // Processed the first check-in broadcast, allow user to tap the finish button.
170
+ refreshUi (true );
171
+ }
172
+ }
118
173
}
0 commit comments