Example usage for android.content.pm ServiceInfo FLAG_STOP_WITH_TASK

List of usage examples for android.content.pm ServiceInfo FLAG_STOP_WITH_TASK

Introduction

In this page you can find the example usage for android.content.pm ServiceInfo FLAG_STOP_WITH_TASK.

Prototype

int FLAG_STOP_WITH_TASK

To view the source code for android.content.pm ServiceInfo FLAG_STOP_WITH_TASK.

Click Source Link

Document

Bit in #flags : If set, the service will automatically be stopped by the system if the user removes a task that is rooted in one of the application's activities.

Usage

From source file:android.content.pm.PackageParser.java

private Service parseService(Package owner, Resources res, XmlPullParser parser, AttributeSet attrs, int flags,
        String[] outError) throws XmlPullParserException, IOException {
    TypedArray sa = res.obtainAttributes(attrs, com.android.internal.R.styleable.AndroidManifestService);

    if (mParseServiceArgs == null) {
        mParseServiceArgs = new ParseComponentArgs(owner, outError,
                com.android.internal.R.styleable.AndroidManifestService_name,
                com.android.internal.R.styleable.AndroidManifestService_label,
                com.android.internal.R.styleable.AndroidManifestService_icon,
                com.android.internal.R.styleable.AndroidManifestService_logo,
                com.android.internal.R.styleable.AndroidManifestService_banner, mSeparateProcesses,
                com.android.internal.R.styleable.AndroidManifestService_process,
                com.android.internal.R.styleable.AndroidManifestService_description,
                com.android.internal.R.styleable.AndroidManifestService_enabled);
        mParseServiceArgs.tag = "<service>";
    }//w w  w  .  ja  v  a  2  s .  c  om

    mParseServiceArgs.sa = sa;
    mParseServiceArgs.flags = flags;

    Service s = new Service(mParseServiceArgs, new ServiceInfo());
    if (outError[0] != null) {
        sa.recycle();
        return null;
    }

    boolean setExported = sa.hasValue(com.android.internal.R.styleable.AndroidManifestService_exported);
    if (setExported) {
        s.info.exported = sa.getBoolean(com.android.internal.R.styleable.AndroidManifestService_exported,
                false);
    }

    String str = sa
            .getNonConfigurationString(com.android.internal.R.styleable.AndroidManifestService_permission, 0);
    if (str == null) {
        s.info.permission = owner.applicationInfo.permission;
    } else {
        s.info.permission = str.length() > 0 ? str.toString().intern() : null;
    }

    s.info.flags = 0;
    if (sa.getBoolean(com.android.internal.R.styleable.AndroidManifestService_stopWithTask, false)) {
        s.info.flags |= ServiceInfo.FLAG_STOP_WITH_TASK;
    }
    if (sa.getBoolean(com.android.internal.R.styleable.AndroidManifestService_isolatedProcess, false)) {
        s.info.flags |= ServiceInfo.FLAG_ISOLATED_PROCESS;
    }
    if (sa.getBoolean(com.android.internal.R.styleable.AndroidManifestService_singleUser, false)) {
        s.info.flags |= ServiceInfo.FLAG_SINGLE_USER;
        if (s.info.exported && (flags & PARSE_IS_PRIVILEGED) == 0) {
            Slog.w(TAG, "Service exported request ignored due to singleUser: " + s.className + " at "
                    + mArchiveSourcePath + " " + parser.getPositionDescription());
            s.info.exported = false;
            setExported = true;
        }
    }

    sa.recycle();

    if ((owner.applicationInfo.privateFlags & ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
        // A heavy-weight application can not have services in its main process
        // We can do direct compare because we intern all strings.
        if (s.info.processName == owner.packageName) {
            outError[0] = "Heavy-weight applications can not have services in main process";
            return null;
        }
    }

    int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
            continue;
        }

        if (parser.getName().equals("intent-filter")) {
            ServiceIntentInfo intent = new ServiceIntentInfo(s);
            if (!parseIntent(res, parser, attrs, true, false, intent, outError)) {
                return null;
            }

            s.intents.add(intent);
        } else if (parser.getName().equals("meta-data")) {
            if ((s.metaData = parseMetaData(res, parser, attrs, s.metaData, outError)) == null) {
                return null;
            }
        } else {
            if (!RIGID_PARSER) {
                Slog.w(TAG, "Unknown element under <service>: " + parser.getName() + " at " + mArchiveSourcePath
                        + " " + parser.getPositionDescription());
                XmlUtils.skipCurrentTag(parser);
                continue;
            } else {
                outError[0] = "Bad element under <service>: " + parser.getName();
                return null;
            }
        }
    }

    if (!setExported) {
        s.info.exported = s.intents.size() > 0;
    }

    return s;
}