summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/ProfileManagerService.java
blob: 73fb4cf7fbe0938053b09506259e34ba27832bfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (c) 2011, 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 com.android.server;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.IProfileManager;
import android.app.NotificationGroup;
import android.app.Profile;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/** {@hide} */
public class ProfileManagerService extends IProfileManager.Stub {

    private static final String PROFILE_FILENAME = "/data/system/profiles.xml";

    private static final String TAG = "ProfileService";

    private Map<String, Profile> mProfiles = new HashMap<String, Profile>();

    private Map<String, NotificationGroup> mGroups = new HashMap<String, NotificationGroup>();

    private Profile mActiveProfile;

    private Context mContext;

    public ProfileManagerService(Context context) {
        mContext = context;
        try {
            loadFromFile();
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            try {
                initialiseStructure();
            } catch (Throwable ex) {
                Log.e(TAG, "Error loading xml from resource: ", ex);
            }
        } catch (IOException e) {
            try {
                initialiseStructure();
            } catch (Throwable ex) {
                Log.e(TAG, "Error loading xml from resource: ", ex);
            }
        }
    }

    // TODO: Could do with returning true/false to convert to exception
    // TODO: Exceptions not supported in aidl.
    @Override
    public void setActiveProfile(String profileName) throws RemoteException {
        setActiveProfile(profileName, true);
    }

    private void setActiveProfile(String profileName, boolean doinit) throws RemoteException {
        if(mProfiles.containsKey(profileName)){
            Log.d(TAG, "Set active profile to: " + profileName);
            mActiveProfile = getProfile(profileName);
            if(doinit){
                mActiveProfile.doSelect(mContext);
            }
        }else{
            Log.e(TAG, "Cannot set active profile to: " + profileName + " - does not exist.");
        }
    }

    @Override
    public void addProfile(Profile profile) throws RemoteException {
        // Make sure this profile has all of the correct groups.
        for (NotificationGroup group : mGroups.values()) {
            profile.ensureProfleGroup(group.getName());
        }
        profile.ensureProfleGroup(
                mContext.getString(com.android.internal.R.string.wildcardProfile), true);
        mProfiles.put(profile.getName(), profile);
    }

    @Override
    public Profile getProfile(String profileName) throws RemoteException {
        return mProfiles.get(profileName);
    }

    @Override
    public Profile[] getProfiles() throws RemoteException {
        return mProfiles.values().toArray(new Profile[mProfiles.size()]);
    }

    @Override
    public Profile getActiveProfile() throws RemoteException {
        return mActiveProfile;
    }

    @Override
    public void removeProfile(Profile profile) throws RemoteException {
        mProfiles.remove(profile.getName());
    }

    @Override
    public NotificationGroup[] getNotificationGroups() throws RemoteException {
        return mGroups.values().toArray(new NotificationGroup[mGroups.size()]);
    }

    @Override
    public void addNotificationGroup(NotificationGroup group) throws RemoteException {
        if (mGroups.put(group.getName(), group) == null) {
            // If the above is true, then the ProfileGroup shouldn't exist in
            // the profile. Ensure it is added.
            for (Profile profile : mProfiles.values()) {
                profile.ensureProfleGroup(group.getName());
            }
        }
    }

    @Override
    public void removeNotificationGroup(NotificationGroup group) throws RemoteException {
        mGroups.remove(group.getName());
        // Remove the corresponding ProfileGroup from all the profiles too if
        // they use it.
        for (Profile profile : mProfiles.values()) {
            profile.removeProfileGroup(group.getName());
        }
    }

    @Override
    public NotificationGroup getNotificationGroupForPackage(String pkg) throws RemoteException {
        for (NotificationGroup group : mGroups.values()) {
            if (group.hasPackage(pkg)) {
                return group;
            }
        }
        return null;
    }

    private void loadFromFile() throws RemoteException, XmlPullParserException, IOException {
        XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
        XmlPullParser xpp = xppf.newPullParser();
        FileReader fr = new FileReader(PROFILE_FILENAME);
        xpp.setInput(fr);
        loadXml(xpp);
    }

    private void loadXml(XmlPullParser xpp) throws XmlPullParserException, IOException,
            RemoteException {
        loadXml(xpp, null);
    }

    private void loadXml(XmlPullParser xpp, Context context) throws XmlPullParserException, IOException,
            RemoteException {
        int event = xpp.next();
        String active = null;
        while (event != XmlPullParser.END_TAG || !"profiles".equals(xpp.getName())) {
            if (event == XmlPullParser.START_TAG) {
                String name = xpp.getName();
                if (name.equals("active")) {
                    active = xpp.nextText();
                    Log.d(TAG, "Found active: " + active);
                } else if (name.equals("profile")) {
                    Profile prof = Profile.fromXml(xpp, context);
                    addProfile(prof);
                    // Failsafe if no active found
                    if (active == null) {
                        active = prof.getName();
                    }
                } else if (name.equals("notificationGroup")) {
                    NotificationGroup ng = NotificationGroup.fromXml(xpp, context);
                    addNotificationGroup(ng);
                }
            }
            event = xpp.next();
        }
        // Don't do initialisation on startup. The AudioManager doesn't exist yet
        // and besides, the volume settings will have survived the reboot.
        setActiveProfile(active, false);
    }

    private void initialiseStructure() throws RemoteException, XmlPullParserException, IOException {
        XmlResourceParser xml = mContext.getResources().getXml(
                com.android.internal.R.xml.profile_default);
        try {
            loadXml(xml, mContext);
            persist();
        } finally {
            xml.close();
        }
    }

    private String getXmlString() throws RemoteException {
        StringBuilder builder = new StringBuilder();
        getXmlString(builder);
        return builder.toString();
    }

    private void getXmlString(StringBuilder builder) throws RemoteException {
        builder.append("<profiles>\n<active>" + TextUtils.htmlEncode(getActiveProfile().getName())
                + "</active>\n");
        for (Profile p : mProfiles.values()) {
            p.getXmlString(builder);
        }
        for (NotificationGroup g : mGroups.values()) {
            g.getXmlString(builder);
        }
        builder.append("</profiles>\n");
    }

    @Override
    public void persist() throws RemoteException {
        try {
            FileWriter fw = new FileWriter(PROFILE_FILENAME);
            fw.write(getXmlString());
            fw.close();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    public NotificationGroup getNotificationGroup(String name) throws RemoteException {
        return mGroups.get(name);
    }

}