Skip to content

Commit

Permalink
Merge pull request #1568 from billdawson/timob-7843
Browse files Browse the repository at this point in the history
TIMOB-7843 Detect android bug 2373 in Android 3.0+ as well (previously o...
  • Loading branch information
marshall committed Mar 10, 2012
2 parents e1f0076 + 3c8c846 commit c156042
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package org.appcelerator.titanium;

import java.util.Set;

import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
Expand All @@ -25,13 +23,14 @@
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
* Titanium launch activites have a single TiContext and launch an associated
* Titanium launch activities have a single TiContext and launch an associated
* Javascript URL during onCreate()
*/
public abstract class TiLaunchActivity extends TiBaseActivity
Expand All @@ -46,8 +45,8 @@ public abstract class TiLaunchActivity extends TiBaseActivity
protected TiUrl url;

// For restarting due to android bug 2373 detection.
private boolean noLaunchCategoryDetected = false;
private AlertDialog noLaunchCategoryAlert;
private boolean invalidLaunchDetected = false;
private AlertDialog invalidLaunchAlert;
private PendingIntent restartPendingIntent = null;
private AlarmManager restartAlarmManager = null;
private int restartDelay = 0;
Expand Down Expand Up @@ -108,7 +107,7 @@ protected void onCreate(Bundle savedInstanceState)
}

// Check for android bug 2373.
if (checkMissingLauncher(savedInstanceState)) {
if (checkInvalidLaunch(savedInstanceState)) {
return;
}
}
Expand Down Expand Up @@ -136,38 +135,42 @@ protected void windowCreated()
scriptLoaded();
}

protected boolean checkMissingLauncher(Bundle savedInstanceState)
protected boolean checkInvalidLaunch(Bundle savedInstanceState)
{
Intent intent = getIntent();
if (intent != null) {
TiProperties systemProperties = getTiApp().getSystemProperties();
boolean detectionDisabled = systemProperties.getBool("ti.android.bug2373.disableDetection", false);
if (!detectionDisabled) {
return checkMissingLauncher(intent, savedInstanceState);
return checkInvalidLaunch(intent, savedInstanceState);
}
}
return false;
}

protected boolean checkMissingLauncher(Intent intent, Bundle savedInstanceState)
protected boolean checkInvalidLaunch(Intent intent, Bundle savedInstanceState)
{
noLaunchCategoryDetected = false;
invalidLaunchDetected = false;
String action = intent.getAction();
if (action != null && action.equals(Intent.ACTION_MAIN)) {
Set<String> categories = intent.getCategories();
noLaunchCategoryDetected = true; // Absence of LAUNCHER is the problem.

if (categories != null) {
for(String category : categories) {
if (category.equals(Intent.CATEGORY_LAUNCHER)) {
noLaunchCategoryDetected = false;
break;
// First check: is the category CATEGORY_LAUNCHER missing?
invalidLaunchDetected = !(intent.hasCategory(Intent.CATEGORY_LAUNCHER));

if (!invalidLaunchDetected) {
// One more check, because Android 3.0+ will put in the launch category but leave out
// FLAG_ACTIVITY_RESET_TASK_IF_NEEDED from the flags, which still causes the problem.
// 0x4 is the flag that occurs when we restart because of the missing category/flag, so
// that one is okay as well.
if (Build.VERSION.SDK_INT >= TiC.API_LEVEL_HONEYCOMB && intent.getFlags() != 0x4) {
int desiredFlags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED;
if ((intent.getFlags() & desiredFlags) != desiredFlags) {
invalidLaunchDetected = true;
}
}
}

if (noLaunchCategoryDetected) {
Log.e(TAG, "Android issue 2373 detected (missing intent CATEGORY_LAUNCHER), restarting app. " + this);
if (invalidLaunchDetected) {
Log.e(TAG, "Android issue 2373 detected (missing intent CATEGORY_LAUNCHER or FLAG_ACTIVITY_RESET_TASK_IF_NEEDED), restarting app. " + this);
layout = new TiCompositeLayout(this, window);
setContentView(layout);
TiProperties systemProperties = getTiApp().getSystemProperties();
Expand Down Expand Up @@ -207,12 +210,12 @@ public void onClick(DialogInterface arg0, int arg1) {

String title = systemProperties.getString("ti.android.bug2373.title", "Restart Required");
String buttonText = systemProperties.getString("ti.android.bug2373.buttonText", "Continue");
noLaunchCategoryAlert = new AlertDialog.Builder(this)
invalidLaunchAlert = new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setPositiveButton(buttonText, restartListener)
.setCancelable(false).create();
noLaunchCategoryAlert.show();
invalidLaunchAlert.show();
}
}

Expand Down Expand Up @@ -255,10 +258,10 @@ public void handleMessage(Message msg)

private void doFinishForRestart()
{
if (noLaunchCategoryAlert != null && noLaunchCategoryAlert.isShowing()) {
noLaunchCategoryAlert.cancel();
if (invalidLaunchAlert != null && invalidLaunchAlert.isShowing()) {
invalidLaunchAlert.cancel();
}
noLaunchCategoryAlert = null;
invalidLaunchAlert = null;

if (!isFinishing()) {
finish();
Expand Down Expand Up @@ -294,7 +297,7 @@ protected void onPause()
return;
}

if (noLaunchCategoryDetected) {
if (invalidLaunchDetected) {
doFinishForRestart();
activityOnPause();
return;
Expand All @@ -311,7 +314,7 @@ protected void onStop()
return;
}

if (noLaunchCategoryDetected) {
if (invalidLaunchDetected) {
activityOnStop();
return;
}
Expand All @@ -326,7 +329,7 @@ protected void onStart()
return;
}

if (noLaunchCategoryDetected) {
if (invalidLaunchDetected) {
activityOnStart();
return;
}
Expand All @@ -341,7 +344,7 @@ protected void onResume()
return;
}

if (noLaunchCategoryDetected) {
if (invalidLaunchDetected) {
alertMissingLauncher(); // This also kicks off the finish() and restart.
activityOnResume();
return;
Expand All @@ -355,7 +358,7 @@ protected void onDestroy()
{
TiApplication tiApp = getTiApp();

if (tiApp.isRestartPending() || noLaunchCategoryDetected) {
if (tiApp.isRestartPending() || invalidLaunchDetected) {
activityOnDestroy();
if (restartAlarmManager == null) {
restartActivity(0);
Expand All @@ -364,8 +367,8 @@ protected void onDestroy()
restartAlarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + restartDelay, restartPendingIntent);
restartPendingIntent = null;
restartAlarmManager = null;
noLaunchCategoryAlert = null;
noLaunchCategoryDetected = false;
invalidLaunchAlert = null;
invalidLaunchDetected = false;
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState)
{
TiApplication tiApp = getTiApp();

if (checkMissingLauncher(savedInstanceState)) {
if (checkInvalidLaunch(savedInstanceState)) {
// Android bug 2373 detected and we're going to restart.
return;
}
Expand Down

0 comments on commit c156042

Please sign in to comment.