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

Commit c0067011 authored by Riddle Hsu's avatar Riddle Hsu
Browse files

Don't schedule restart service if it will be bring down

Originally, the log "Scheduling restart of crashed service"
will show even when a service is died with condition:
 (1) Force-stop package
 (2) Exceed max delivery count without connections
 (3) Not sticky without connections
But all of these conditions will bring down the service and
cancel the restart. That is unnecessary and the printed log
is confusing.

(1) is fixed by changing the parameter "allowRestart" to false
when killing the package. This also involves the condition to
restart the provider that should be always-remove instead of
always-restart.
(2) and (3) are fixed by the early return (in the method
ActiveServices#scheduleServiceRestartLocked) that checks if the
service is no longer started and doesn't have connections.

Also print the reason when scheduling restart service so it has
clearer information to know the condition.

The original FrameworksCoreTests:ServiceTest is replaced because
the same tests already exist in CTS.

Bug: 133842309
Test: atest FrameworksCoreTests:ServiceTest
Test: atest CtsAppTestCases:ServiceTest

Change-Id: Iea3a97b10b3872241e069de602450b929cd3fa7b
parent 4b235cb0
Loading
Loading
Loading
Loading
+2 −16
Original line number Original line Diff line number Diff line
@@ -1270,10 +1270,8 @@
                <action android:name="com.android.frameworks.coretests.activity.BROADCAST_REMOTE_DENIED" />
                <action android:name="com.android.frameworks.coretests.activity.BROADCAST_REMOTE_DENIED" />
            </intent-filter>
            </intent-filter>
        </receiver>
        </receiver>
        <service android:name="android.app.activity.LocalService">
        <service android:name="android.app.activity.ServiceTest$RemoteService"
            <intent-filter>
                android:process=":RemoteService">
                <action android:name="com.android.frameworks.coretests.activity.SERVICE_LOCAL" />
            </intent-filter>
            <meta-data android:name="com.android.frameworks.coretests.string" android:value="foo" />
            <meta-data android:name="com.android.frameworks.coretests.string" android:value="foo" />
            <meta-data android:name="com.android.frameworks.coretests.boolean" android:value="true" />
            <meta-data android:name="com.android.frameworks.coretests.boolean" android:value="true" />
            <meta-data android:name="com.android.frameworks.coretests.integer" android:value="100" />
            <meta-data android:name="com.android.frameworks.coretests.integer" android:value="100" />
@@ -1281,18 +1279,6 @@
            <meta-data android:name="com.android.frameworks.coretests.float" android:value="100.1" />
            <meta-data android:name="com.android.frameworks.coretests.float" android:value="100.1" />
            <meta-data android:name="com.android.frameworks.coretests.reference" android:resource="@xml/metadata" />
            <meta-data android:name="com.android.frameworks.coretests.reference" android:resource="@xml/metadata" />
        </service>
        </service>
        <service android:name="android.app.activity.LocalDeniedService"
                android:permission="com.android.frameworks.coretests.permission.TEST_DENIED">
            <intent-filter>
                <action android:name="com.android.frameworks.coretests.activity.SERVICE_LOCAL_DENIED" />
            </intent-filter>
        </service>
        <service android:name="android.app.activity.LocalGrantedService"
                android:permission="com.android.frameworks.coretests.permission.TEST_GRANTED">
            <intent-filter>
                <action android:name="com.android.frameworks.coretests.activity.SERVICE_LOCAL_GRANTED" />
            </intent-filter>
        </service>


        <service
        <service
            android:name="android.service.settings.suggestions.MockSuggestionService"
            android:name="android.service.settings.suggestions.MockSuggestionService"
+0 −22
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.activity;

public class LocalDeniedService extends LocalService
{
}
+0 −22
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.activity;

public class LocalGrantedService extends LocalService
{
}
+2 −2
Original line number Original line Diff line number Diff line
@@ -23,9 +23,9 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter;
import android.content.ReceiverCallNotAllowedException;
import android.content.ReceiverCallNotAllowedException;
import android.content.ServiceConnection;
import android.content.ServiceConnection;
import android.os.RemoteException;
import android.os.IBinder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Parcel;
import android.os.RemoteException;


public class LocalReceiver extends BroadcastReceiver {
public class LocalReceiver extends BroadcastReceiver {
    public LocalReceiver() {
    public LocalReceiver() {
@@ -52,7 +52,7 @@ public class LocalReceiver extends BroadcastReceiver {
                    public void onServiceDisconnected(ComponentName name) {
                    public void onServiceDisconnected(ComponentName name) {
                    }
                    }
                };
                };
                context.bindService(new Intent(context, LocalService.class), sc, 0);
                context.bindService(new Intent(context, ServiceTest.RemoteService.class), sc, 0);
                context.unbindService(sc);
                context.unbindService(sc);
            } catch (ReceiverCallNotAllowedException e) {
            } catch (ReceiverCallNotAllowedException e) {
                //resultString = "This is the correct behavior but not yet implemented";
                //resultString = "This is the correct behavior but not yet implemented";
+0 −122
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app.activity;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.IBinder;
import android.os.Parcel;
import android.util.Log;

public class LocalService extends Service {
    private final IBinder mBinder = new Binder() {

        @Override
        protected boolean onTransact(int code, Parcel data, Parcel reply,
                int flags) throws RemoteException {
            if (code == ServiceTest.SET_REPORTER_CODE) {
                data.enforceInterface(ServiceTest.SERVICE_LOCAL);
                mReportObject = data.readStrongBinder();
                return true;
            } else {
                return super.onTransact(code, data, reply, flags);
            }
        }
        
    };

    private IBinder mReportObject;
    private int mStartCount = 1;

    public LocalService() {
    }

    @Override
    public void onStart(Intent intent, int startId) {
        //Log.i("LocalService", "onStart: " + intent);
        if (intent.getExtras() != null) {
            mReportObject = intent.getExtras().getIBinder(ServiceTest.REPORT_OBJ_NAME);
            if (mReportObject != null) {
                try {
                    Parcel data = Parcel.obtain();
                    data.writeInterfaceToken(ServiceTest.SERVICE_LOCAL);
                    data.writeInt(mStartCount);
                    mStartCount++;
                    mReportObject.transact(
                            ServiceTest.STARTED_CODE, data, null, 0);
                    data.recycle();
                } catch (RemoteException e) {
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        Log.i("LocalService", "onDestroy: mReportObject=" + mReportObject);
        if (mReportObject != null) {
            try {
                Parcel data = Parcel.obtain();
                data.writeInterfaceToken(ServiceTest.SERVICE_LOCAL);
                mReportObject.transact(
                        ServiceTest.DESTROYED_CODE, data, null, 0);
                data.recycle();
            } catch (RemoteException e) {
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("LocalService", "onBind: " + intent);
        return mBinder;
    }
    
    @Override
    public boolean onUnbind(Intent intent) {
        Log.i("LocalService", "onUnbind: " + intent);
        if (mReportObject != null) {
            try {
                Parcel data = Parcel.obtain();
                data.writeInterfaceToken(ServiceTest.SERVICE_LOCAL);
                mReportObject.transact(
                        ServiceTest.UNBIND_CODE, data, null, 0);
                data.recycle();
            } catch (RemoteException e) {
            }
        }
        return true;
    }
    
    @Override
    public void onRebind(Intent intent) {
        Log.i("LocalService", "onUnbind: " + intent);
        if (mReportObject != null) {
            try {
                Parcel data = Parcel.obtain();
                data.writeInterfaceToken(ServiceTest.SERVICE_LOCAL);
                mReportObject.transact(
                        ServiceTest.REBIND_CODE, data, null, 0);
                data.recycle();
            } catch (RemoteException e) {
            }
        }
    }
}
Loading