Titanium JIRA Archive
Appcelerator Community (AC)

[AC-3920] Android crashes when uninstalling titanium app using module calling Wearable.DataApi.putDataItem(client, request).await();

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionNeeds more info
Resolution Date2016-07-14T19:10:49.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsandoid, module, titanium
ReporterTeun Klijn
AssigneeShak Hossain
Created2016-06-24T07:41:54.000+0000
Updated2016-07-14T19:10:49.000+0000

Description

I've created a module for Titanium to send data from my app to my Android Wear application which works great. But when I uninstall the application Android crashes. I've checked Logcat but I don't see what the problem exactly is. Android only seems to crash if I uninstall the application right after calling module.sendData() (see example) Example: *index.js (app)*
function onOpen() {
  var module = require('teunozz.test.module')
	
  module.addEventListener('connected', function f() {
	console.log('send data');
	module.sendData();
   });

  module.start();
}

$.index.open();
*index.xml (app)*
<Alloy>
  <Window id="index" onOpen="onOpen">
  </Window>
</Alloy>
*AndroidWearClient (module)*
package teunozz.test.module;
    
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.util.TiActivityResultHandler;
    
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
    
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;
    
public class AndroidWearClient extends Object implements	
  TiActivityResultHandler,
  GoogleApiClient.ConnectionCallbacks,
  GoogleApiClient.OnConnectionFailedListener
 {
   	private final static String TAG = "AndroidWearClient";
    
        private static final int REQUEST_OAUTH = 1;
        private GoogleApiClient client = null;
        private TitestModule module = null;
        private boolean authInProgress = false;
        
        private void buildClient() {
        	client = new GoogleApiClient.Builder(
    				TiApplication.getAppCurrentActivity())
    		    	.addApi(Wearable.API)
    				.addConnectionCallbacks(this)
    				.addOnConnectionFailedListener(this)
    				.build();
        }
        
        public void start(TitestModule moduleArg) {
            module = moduleArg;
            buildClient();
            client.connect();
        }
        
        public void stop() {
        }
        
    	public void onError(Activity activity, int requestCode, Exception e) {
    	}
    
    	public void onResult(Activity activity, int requestCode, int resultCode, Intent data) {		
    		if (requestCode == REQUEST_OAUTH) {
    			authInProgress = false;
                if (resultCode == Activity.RESULT_OK) {
                    if (!client.isConnecting() && !client.isConnected()) {
                        client.connect();
                    }
                }
            }
    	}
    	
    	public void onConnectionFailed(ConnectionResult result) {
    	}
    
    	public void onConnected(Bundle connectionHint) {
    		// connected to Google Wearable API
    		module.fireEvent("connected", null);
    	}
        
    	public void onConnectionSuspended(int cause) {
    	}
        
        public void sendData() {
    		// Construct a DataRequest and send over the data layer
    		PutDataMapRequest putDMR = PutDataMapRequest.create("/test");
    		putDMR.getDataMap().putString("key", "test");
    		PutDataRequest request = putDMR.asPutDataRequest();
    		DataApi.DataItemResult result = 
            // this is the problematic bit if you comment this out  Android doesn't crash
            Wearable.DataApi.putDataItem(client, request).await();
    		
    		if (result.getStatus().isSuccess()) {
        		// Do something
    			Log.e(TAG, "SUCCESS");
    		} else {
    			// Do something
    		}
        }    
    }

*TitestModule (module)*
package teunozz.test.module;
    
    import teunozz.test.module.AndroidWearClient;
    
    import org.appcelerator.kroll.KrollDict;
    import org.appcelerator.kroll.KrollModule;
    import org.appcelerator.kroll.annotations.Kroll;
    import org.appcelerator.titanium.TiApplication;
    import org.appcelerator.titanium.util.TiIntentWrapper;
    import org.appcelerator.kroll.common.Log;
    import org.appcelerator.kroll.common.TiConfig;
    
    import android.app.Activity;
    import android.content.Intent;    
    
    @Kroll.module(name="Titest", id="teunozz.test.module")
    public class TitestModule extends KrollModule
    {
    
    	// Standard Debugging variables
    	private static final String LCAT = "TitestModule";
    	private static final boolean DBG = TiConfig.LOGD;
    	
    	private AndroidWearClient client = new AndroidWearClient();
    
    	// You can define constants with @Kroll.constant, for example:
    	// @Kroll.constant public static final String EXTERNAL_NAME = value;
    
    	public TitestModule()
    	{
    		super();
    	}
    
    	@Kroll.onAppCreate
    	public static void onAppCreate(TiApplication app)
    	{
    		// put module init code that needs to run when the application is created
    	}
    	
    	@Override
    	public void onDestroy(Activity activity) {	
    		client.stop();
    	}
    	
    	@Kroll.method
    	public void start() {
    
    		TiApplication appContext = TiApplication.getInstance();
    		Activity activity = appContext.getCurrentActivity();
    		
    		client.start(this);
    	}
    
        @Kroll.method
    	public void stop() {
        	client.stop();
        }
    
        @Kroll.method
    	public void sendData() {
        	client.sendData();
        }
    }
The basic module code and distribution is available on GitHub: https://github.com/Teunozz/titest This is the first error that I can find in Logcat.
06-20 13:36:06.599: E/AndroidRuntime(14455): *** FATAL EXCEPTION IN SYSTEM PROCESS: NetworkPolicy
    06-20 13:36:06.599: E/AndroidRuntime(14455): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.os.action.POWER_SAVE_TEMP_WHITELIST_CHANGED flg=0x40000010 bqHint=4 } in com.android.server.net.NetworkPolicyManagerService$4@bd55cc8
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1003)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at android.os.Handler.handleCallback(Handler.java:739)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at android.os.Handler.dispatchMessage(Handler.java:95)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at android.os.Looper.loop(Looper.java:158)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at android.os.HandlerThread.run(HandlerThread.java:61)
    06-20 13:36:06.599: E/AndroidRuntime(14455): Caused by: java.lang.NullPointerException: Attempt to get length of null array
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at com.android.server.net.NetworkPolicyManagerService.isUidIdle(NetworkPolicyManagerService.java:3471)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at com.android.server.net.NetworkPolicyManagerService.updateRuleForAppIdleLocked(NetworkPolicyManagerService.java:3397)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at com.android.server.net.NetworkPolicyManagerService.updateRulesForTempWhitelistChangeLocked(NetworkPolicyManagerService.java:3451)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at com.android.server.net.NetworkPolicyManagerService$4.onReceive(NetworkPolicyManagerService.java:731)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:993)
    06-20 13:36:06.599: E/AndroidRuntime(14455): 	... 4 more
    06-20 13:36:06.659: E/android.os.Debug(14455): ro.product_ship = true
    06-20 13:36:06.659: E/android.os.Debug(14455): ro.debug_level = 0x4f4c
    06-20 13:36:06.659: E/android.os.Debug(14455): sys.mobilecare.preload = false
    ...
After this every service dies and Android restarts.
    06-20 13:36:07.049: I/ServiceManager(3046): service 'wifip2p' died
    06-20 13:36:07.059: I/ServiceManager(3046): service 'package' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'user' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'activity' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'procstats' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'meminfo' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'gfxinfo' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'dbinfo' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'cpuinfo' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'permission' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'processinfo' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'sensorservice' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'lock_settings' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'mdm.remotedesktop' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'battery' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'usagestats' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'webviewupdate' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'scheduling_policy' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'telephony.registry' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'persona' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'deviceidle' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'application_policy' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'wifi_policy' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'phone_restriction_policy' died
    06-20 13:36:07.069: I/ServiceManager(3046): service 'remoteinjection' died
    ...

Comments

  1. Sharif AbuDarda 2016-06-30

    Hello, the instruction is not clear, Can you please explain how I can test this in my environment. Please provide a compleat project that I use to regenerate the issue. Also, Please provide complete list of steps to follow.

JSON Source