Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 128edf86 authored by Yurii Zubrytskyi's avatar Yurii Zubrytskyi Committed by Automerger Merge Worker
Browse files

Merge "[incfs] Cleanup data loader connection and fds in Java" into rvc-dev am: 2ccbf249

Change-Id: I8fc8857ba3ca116e5ba22988e77e291448d21299
parents 33fc705d 2ccbf249
Loading
Loading
Loading
Loading
+79 −26
Original line number Original line Diff line number Diff line
@@ -38,6 +38,8 @@ import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.GuardedBy;
import com.android.server.SystemService;
import com.android.server.SystemService;


import libcore.io.IoUtils;

import java.util.List;
import java.util.List;


/**
/**
@@ -64,24 +66,38 @@ public class DataLoaderManagerService extends SystemService {
        publishBinderService(Context.DATA_LOADER_MANAGER_SERVICE, mBinderService);
        publishBinderService(Context.DATA_LOADER_MANAGER_SERVICE, mBinderService);
    }
    }


    private static void closeQuietly(FileSystemControlParcel control) {
        if (control == null || control.incremental == null) {
            return;
        }
        IoUtils.closeQuietly(control.incremental.cmd);
        IoUtils.closeQuietly(control.incremental.pendingReads);
        IoUtils.closeQuietly(control.incremental.log);
    }

    final class DataLoaderManagerBinderService extends IDataLoaderManager.Stub {
    final class DataLoaderManagerBinderService extends IDataLoaderManager.Stub {
        @Override
        @Override
        public boolean initializeDataLoader(int dataLoaderId, DataLoaderParamsParcel params,
        public boolean initializeDataLoader(int dataLoaderId, DataLoaderParamsParcel params,
                FileSystemControlParcel control, IDataLoaderStatusListener listener) {
                FileSystemControlParcel control, IDataLoaderStatusListener listener) {
            DataLoaderServiceConnection connection = null;
            try {
                synchronized (mLock) {
                synchronized (mLock) {
                    if (mServiceConnections.get(dataLoaderId) != null) {
                    if (mServiceConnections.get(dataLoaderId) != null) {
                        Slog.e(TAG, "Data loader of ID=" + dataLoaderId + " already exists.");
                        Slog.e(TAG, "Data loader of ID=" + dataLoaderId + " already exists.");
                        return false;
                        return false;
                    }
                    }
                }
                }
            ComponentName componentName = new ComponentName(params.packageName, params.className);
                ComponentName componentName =
                        new ComponentName(params.packageName, params.className);
                ComponentName dataLoaderComponent = resolveDataLoaderComponentName(componentName);
                ComponentName dataLoaderComponent = resolveDataLoaderComponentName(componentName);
                if (dataLoaderComponent == null) {
                if (dataLoaderComponent == null) {
                    return false;
                    return false;
                }
                }
                // Binds to the specific data loader service
                // Binds to the specific data loader service
            DataLoaderServiceConnection connection =
                connection =
                    new DataLoaderServiceConnection(dataLoaderId, params, control, listener);
                        new DataLoaderServiceConnection(dataLoaderId, params,
                                                        control, listener);
                control = null; // now connection manages it
                Intent intent = new Intent();
                Intent intent = new Intent();
                intent.setComponent(dataLoaderComponent);
                intent.setComponent(dataLoaderComponent);
                if (!mContext.bindServiceAsUser(intent, connection, Context.BIND_AUTO_CREATE,
                if (!mContext.bindServiceAsUser(intent, connection, Context.BIND_AUTO_CREATE,
@@ -90,6 +106,13 @@ public class DataLoaderManagerService extends SystemService {
                    mContext.unbindService(connection);
                    mContext.unbindService(connection);
                    return false;
                    return false;
                }
                }
                connection = null;
            } finally {
                DataLoaderManagerService.closeQuietly(control);
                if (connection != null) {
                    connection.close();
                }
            }
            return true;
            return true;
        }
        }


@@ -173,7 +196,7 @@ public class DataLoaderManagerService extends SystemService {
        }
        }
    }
    }


    class DataLoaderServiceConnection implements ServiceConnection {
    class DataLoaderServiceConnection implements ServiceConnection, AutoCloseable {
        final int mId;
        final int mId;
        final DataLoaderParamsParcel mParams;
        final DataLoaderParamsParcel mParams;
        final FileSystemControlParcel mControl;
        final FileSystemControlParcel mControl;
@@ -204,15 +227,34 @@ public class DataLoaderManagerService extends SystemService {


        @Override
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
        public void onServiceDisconnected(ComponentName arg0) {
            if (mListener != null) {
            Slog.i(TAG, "DataLoader " + mId + " disconnected, but will try to recover");
                try {
            callListener(IDataLoaderStatusListener.DATA_LOADER_DESTROYED);
                    mListener.onStatusChanged(mId, IDataLoaderStatusListener.DATA_LOADER_DESTROYED);
            remove();
                } catch (RemoteException ignored) {
        }
        }

        @Override
        public void onBindingDied(ComponentName name) {
            Slog.i(TAG, "DataLoader " + mId + " died");
            callListener(IDataLoaderStatusListener.DATA_LOADER_DESTROYED);
            mContext.unbindService(this);
            close();
            remove();
        }
        }

        @Override
        public void onNullBinding(ComponentName name) {
            Slog.i(TAG, "DataLoader " + mId + " failed to start");
            callListener(IDataLoaderStatusListener.DATA_LOADER_DESTROYED);
            mContext.unbindService(this);
            close();
            remove();
            remove();
        }
        }


        @Override
        public void close() {
            DataLoaderManagerService.closeQuietly(mControl);
        }

        IDataLoader getDataLoader() {
        IDataLoader getDataLoader() {
            return mDataLoader;
            return mDataLoader;
        }
        }
@@ -223,6 +265,8 @@ public class DataLoaderManagerService extends SystemService {
            } catch (RemoteException ignored) {
            } catch (RemoteException ignored) {
            }
            }
            mContext.unbindService(this);
            mContext.unbindService(this);
            close();
            remove();
        }
        }


        private void remove() {
        private void remove() {
@@ -230,5 +274,14 @@ public class DataLoaderManagerService extends SystemService {
                mServiceConnections.remove(mId);
                mServiceConnections.remove(mId);
            }
            }
        }
        }

        private void callListener(int status) {
            if (mListener != null) {
                try {
                    mListener.onStatusChanged(mId, status);
                } catch (RemoteException ignored) {
                }
            }
        }
    }
    }
}
}