MainActivity: try to fix asynchronous setup issues

try to determine the availability of work profile even if the profile is still marked as being setting up
This commit is contained in:
Peter Cai 2018-09-01 20:56:22 +08:00
parent f2e97117a2
commit 126a7c5f53
No known key found for this signature in database
GPG key ID: 71F5FB4E4F3FD54F

View file

@ -86,7 +86,7 @@ public class MainActivity extends AppCompatActivity {
} }
private void init() { private void init() {
if (mStorage.getBoolean(LocalStorageManager.PREF_IS_SETTING_UP)) { if (mStorage.getBoolean(LocalStorageManager.PREF_IS_SETTING_UP) && !isWorkProfileAvailable()) {
// Provision is still going on... // Provision is still going on...
Toast.makeText(this, R.string.provision_still_pending, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.provision_still_pending, Toast.LENGTH_SHORT).show();
finish(); finish();
@ -129,7 +129,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
public void onServiceConnected(ComponentName name, IBinder service) { public void onServiceConnected(ComponentName name, IBinder service) {
mServiceMain = IShelterService.Stub.asInterface(service); mServiceMain = IShelterService.Stub.asInterface(service);
detectWorkProfileAvailability(); tryStartWorkService();
} }
@Override @Override
@ -139,9 +139,9 @@ public class MainActivity extends AppCompatActivity {
}, false); }, false);
} }
private void detectWorkProfileAvailability() { private void tryStartWorkService() {
// Send a dummy intent to the work profile first // Send a dummy intent to the work profile first
// to determine if work mode is enabled. // to determine if work mode is enabled and we CAN start something in that profile.
// If work mode is disabled when starting this app, we will receive RESULT_CANCELED // If work mode is disabled when starting this app, we will receive RESULT_CANCELED
// in the activity result. // in the activity result.
Intent intent = new Intent(DummyActivity.TRY_START_SERVICE); Intent intent = new Intent(DummyActivity.TRY_START_SERVICE);
@ -193,6 +193,23 @@ public class MainActivity extends AppCompatActivity {
mTabs.setupWithViewPager(mPager); mTabs.setupWithViewPager(mPager);
} }
private boolean isWorkProfileAvailable() {
// Determine if the work profile is already available
// If so, return true and set all the corresponding flags to true
// This is for scenarios where the asynchronous part of the
// setup process might be finished before the synchronous part
Intent intent = new Intent(DummyActivity.TRY_START_SERVICE);
try {
Utility.transferIntentToProfile(this, intent);
mStorage.setBoolean(LocalStorageManager.PREF_IS_SETTING_UP, false);
mStorage.setBoolean(LocalStorageManager.PREF_HAS_SETUP, true);
return true;
} catch (IllegalStateException e) {
// If any exception is thrown, this means that the profile is not available
return false;
}
}
// Get the service on the other side // Get the service on the other side
// remote (work) -> main // remote (work) -> main
// main -> remote (work) // main -> remote (work)
@ -316,8 +333,9 @@ public class MainActivity extends AppCompatActivity {
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (requestCode == REQUEST_PROVISION_PROFILE) { if (requestCode == REQUEST_PROVISION_PROFILE) {
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { if (isWorkProfileAvailable()) {
// For pre-Oreo, by the time this is received, the whole process // For pre-Oreo, or post-Oreo on some circumstances,
// by the time this is received, the whole process
// should have completed. // should have completed.
recreate(); recreate();
return; return;