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