ShortcutPackageItem.java revision 9da23fc6ac565b38129d52f4f8f174c833a9bd01
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.server.pm;
17
18import android.annotation.NonNull;
19
20import com.android.internal.util.Preconditions;
21
22import org.xmlpull.v1.XmlPullParserException;
23import org.xmlpull.v1.XmlSerializer;
24
25import java.io.IOException;
26
27abstract class ShortcutPackageItem {
28    private final int mPackageUserId;
29    private final String mPackageName;
30
31    private ShortcutPackageInfo mPackageInfo;
32
33    protected ShortcutPackageItem(int packageUserId, @NonNull String packageName,
34            @NonNull ShortcutPackageInfo packageInfo) {
35        mPackageUserId = packageUserId;
36        mPackageName = Preconditions.checkStringNotEmpty(packageName);
37        mPackageInfo = Preconditions.checkNotNull(packageInfo);
38    }
39
40    /**
41     * ID of the user who actually has this package running on.  For {@link ShortcutPackage},
42     * this is the same thing as {@link #getOwnerUserId}, but if it's a {@link ShortcutLauncher} and
43     * {@link #getOwnerUserId} is of a work profile, then this ID could be the user who owns the
44     * profile.
45     */
46    public int getPackageUserId() {
47        return mPackageUserId;
48    }
49
50    /**
51     * ID of the user who sees the shortcuts from this instance.
52     */
53    public abstract int getOwnerUserId();
54
55    @NonNull
56    public String getPackageName() {
57        return mPackageName;
58    }
59
60    public ShortcutPackageInfo getPackageInfo() {
61        return mPackageInfo;
62    }
63
64    /**
65     * Should be only used when loading from a file.o
66     */
67    protected void replacePackageInfo(@NonNull ShortcutPackageInfo packageInfo) {
68        mPackageInfo = Preconditions.checkNotNull(packageInfo);
69    }
70
71    public void refreshPackageInfoAndSave(ShortcutService s) {
72        mPackageInfo.refresh(s, this);
73        s.scheduleSaveUser(getOwnerUserId());
74    }
75
76    public void ensureNotShadowAndSave(ShortcutService s) {
77        if (mPackageInfo.isShadow()) {
78            mPackageInfo.setShadow(false);
79            s.scheduleSaveUser(getOwnerUserId());
80        }
81    }
82
83    public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
84            throws IOException, XmlPullParserException;
85}
86