@@ -51,7 +51,7 @@ public class AidlNetworkRequest extends NetworkRequest {
5151 */
5252 private ServiceConnection mConnection = new ServiceConnection () {
5353 public void onServiceConnected (ComponentName className , IBinder service ) {
54- Log .v (TAG , "Nextcloud Single sign-on: onServiceConnected [" + Thread .currentThread ().getName () + "]" );
54+ Log .d (TAG , "[onServiceConnected] called from Thread: [" + Thread .currentThread ().getName () + "] with IBinder [" + className . toString () + "]: " + service );
5555
5656 mService = IInputStreamService .Stub .asInterface (service );
5757 mBound .set (true );
@@ -62,56 +62,75 @@ public void onServiceConnected(ComponentName className, IBinder service) {
6262 }
6363
6464 public void onServiceDisconnected (ComponentName className ) {
65- Log .e (TAG , "Nextcloud Single sign-on: ServiceDisconnected " );
65+ Log .w (TAG , "[onServiceDisconnected] [" + className . toString () + "] " );
6666 // This is called when the connection with the service has been
67- // unexpectedly disconnected -- that is, its process crashed.
68- mService = null ;
69- mBound .set (false );
67+ // unexpectedly disconnected -- that is, its process crashed or the service was
68+ // terminated due to an update (e.g. google play store)
7069
7170 if (!mDestroyed ) {
72- connectApiWithBackoff ();
71+ // In case we're currently not reconnecting
72+ Log .d (TAG , "[onServiceDisconnected] Reconnecting lost service connection to component: [" + className .toString () + "]" );
73+ reconnect ();
74+ } else {
75+ // API was destroyed on purpose
76+ mService = null ;
77+ mBound .set (false );
7378 }
7479 }
7580 };
7681
7782 public void connect (String type ) {
78- if (mDestroyed ) {
79- throw new IllegalStateException ("API already stopped" );
80- }
81-
83+ Log .d (TAG , "[connect] Binding to AccountManagerService for type [" + type + "]" );
8284 super .connect (type );
8385
8486 String componentName = Constants .PACKAGE_NAME_PROD ;
8587 if (type .equals (Constants .ACCOUNT_TYPE_DEV )) {
8688 componentName = Constants .PACKAGE_NAME_DEV ;
8789 }
8890
91+ Log .d (TAG , "[connect] Component name is: [" + componentName + "]" );
92+
8993 try {
9094 Intent intentService = new Intent ();
9195 intentService .setComponent (new ComponentName (componentName ,
9296 "com.owncloud.android.services.AccountManagerService" ));
93- if (!mContext .bindService (intentService , mConnection , Context .BIND_AUTO_CREATE )) {
94- Log .d (TAG , "Binding to AccountManagerService returned false" );
97+ // https://developer.android.com/reference/android/content/Context#BIND_ABOVE_CLIENT
98+ if (!mContext .bindService (intentService , mConnection , Context .BIND_AUTO_CREATE | Context .BIND_ABOVE_CLIENT )) {
99+ Log .d (TAG , "[connect] Binding to AccountManagerService returned false" );
95100 throw new IllegalStateException ("Binding to AccountManagerService returned false" );
101+ } else {
102+ Log .d (TAG , "[connect] Bound to AccountManagerService successfully" );
96103 }
97104 } catch (SecurityException e ) {
98- Log .e (TAG , "can't bind to AccountManagerService, check permission in Manifest" );
105+ Log .e (TAG , "[connect] can't bind to AccountManagerService, check permission in Manifest" );
99106 mCallback .onError (e );
100107 }
101108 }
102109
110+ public void reconnect () {
111+ Log .d (TAG , "[reconnect] called" );
112+ unbindService ();
113+ connectApiWithBackoff ();
114+ }
115+
103116 public void stop () {
104117 super .stop ();
105118
119+ unbindService ();
120+ mContext = null ;
121+ }
122+
123+ private void unbindService () {
106124 // Unbind from the service
107125 if (mBound .get ()) {
108126 if (mContext != null ) {
127+ Log .d (TAG , "[unbindService] Unbinding AccountManagerService" );
109128 mContext .unbindService (mConnection );
110129 } else {
111- Log .e (TAG , "Context was null, cannot unbind nextcloud single sign-on service connection!" );
130+ Log .e (TAG , "[unbindService] Context was null, cannot unbind nextcloud single sign-on service connection!" );
112131 }
113132 mBound .set (false );
114- mContext = null ;
133+ mService = null ;
115134 }
116135 }
117136
@@ -123,7 +142,7 @@ private void waitForApi() throws NextcloudApiNotRespondingException {
123142 try {
124143 mBound .wait (10000 ); // wait up to 10 seconds
125144
126- // If api is still not bound after 10 seconds.. throw an exception
145+ // If api is still not bound after 10 seconds.. try reconnecting
127146 if (!mBound .get ()) {
128147 throw new NextcloudApiNotRespondingException ();
129148 }
@@ -143,8 +162,6 @@ private void waitForApi() throws NextcloudApiNotRespondingException {
143162 * @throws Exception or SSOException
144163 */
145164 public Response performNetworkRequestV2 (NextcloudRequest request , InputStream requestBodyInputStream ) throws Exception {
146-
147-
148165 ParcelFileDescriptor output = performAidlNetworkRequestV2 (request , requestBodyInputStream );
149166 InputStream os = new ParcelFileDescriptor .AutoCloseInputStream (output );
150167 ExceptionResponse response = deserializeObjectV2 (os );
@@ -204,6 +221,10 @@ private ParcelFileDescriptor performAidlNetworkRequest(NextcloudRequest request,
204221 throw new NetworkOnMainThreadException ();
205222 }
206223
224+ if (mDestroyed ) {
225+ throw new IllegalStateException ("Nextcloud API already destroyed. Please report this issue." );
226+ }
227+
207228 // Wait for api to be initialized
208229 waitForApi ();
209230
0 commit comments