1 /*
2  * Copyright (C) 2013 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  */
16 
17 package com.android.settings.print;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.provider.Settings;
22 import android.text.TextUtils.SimpleStringSplitter;
23 
24 import java.util.ArrayList;import java.util.List;
25 
26 /**
27  * Helper methods for reading and writing to print settings.
28  */
29 public class PrintSettingsUtils {
30 
31     private static final char ENABLED_PRINT_SERVICES_SEPARATOR = ':';
32 
PrintSettingsUtils()33     private PrintSettingsUtils() {
34         /* do nothing */
35     }
36 
readEnabledPrintServices(Context context)37     public static List<ComponentName> readEnabledPrintServices(Context context) {
38         List<ComponentName> enabledServices = new ArrayList<ComponentName>();
39 
40         String enabledServicesSetting = Settings.Secure.getString(context
41                 .getContentResolver(), Settings.Secure.ENABLED_PRINT_SERVICES);
42         if (enabledServicesSetting == null) {
43             return enabledServices;
44         }
45 
46         SimpleStringSplitter colonSplitter = new SimpleStringSplitter(
47                 ENABLED_PRINT_SERVICES_SEPARATOR);
48         colonSplitter.setString(enabledServicesSetting);
49 
50         while (colonSplitter.hasNext()) {
51             String componentNameString = colonSplitter.next();
52             ComponentName enabledService = ComponentName.unflattenFromString(
53                     componentNameString);
54             enabledServices.add(enabledService);
55         }
56 
57         return enabledServices;
58     }
59 
writeEnabledPrintServices(Context context, List<ComponentName> services)60     public static void writeEnabledPrintServices(Context context,
61             List<ComponentName> services) {
62         StringBuilder builder = new StringBuilder();
63         final int serviceCount = services.size();
64         for (int i = 0; i < serviceCount; i++) {
65             ComponentName service = services.get(i);
66             if (builder.length() > 0) {
67                 builder.append(ENABLED_PRINT_SERVICES_SEPARATOR);
68             }
69             builder.append(service.flattenToString());
70         }
71         Settings.Secure.putString(context.getContentResolver(),
72                 Settings.Secure.ENABLED_PRINT_SERVICES,
73                 builder.toString());
74     }
75 }
76