1 /* 2 * Copyright (C) 2016 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.googlecode.android_scripting.facade; 18 19 import android.app.Service; 20 import android.content.SharedPreferences; 21 import android.content.SharedPreferences.Editor; 22 import android.preference.PreferenceManager; 23 24 import com.googlecode.android_scripting.jsonrpc.RpcReceiver; 25 import com.googlecode.android_scripting.rpc.Rpc; 26 import com.googlecode.android_scripting.rpc.RpcOptional; 27 import com.googlecode.android_scripting.rpc.RpcParameter; 28 29 import java.io.IOException; 30 import java.util.Map; 31 32 /** 33 * This facade allows access to the Preferences interface. 34 * 35 * <br> 36 * <b>Notes:</b> <br> 37 * <b>filename</b> - Filename indicates which preference file to refer to. If no filename is 38 * supplied (the default) then the SharedPreferences uses is the default for the SL4A application.<br> 39 * <b>prefPutValue</b> - uses "MODE_PRIVATE" when writing to preferences. Save values to the default 40 * shared preferences is explicitly disallowed.<br> 41 * <br> 42 * See <a 43 * href=http://developer.android.com/reference/java/util/prefs/Preferences.html>Preferences</a> and 44 * <a href=http://developer.android.com/guide/topics/data/data-storage.html#pref>Shared 45 * Preferences</a> in the android documentation on how preferences work. 46 * 47 * @author Robbie Matthews (rjmatthews62@gmail.com) 48 */ 49 50 public class PreferencesFacade extends RpcReceiver { 51 52 private Service mService; 53 PreferencesFacade(FacadeManager manager)54 public PreferencesFacade(FacadeManager manager) { 55 super(manager); 56 mService = manager.getService(); 57 } 58 59 @Rpc(description = "Read a value from shared preferences") prefGetValue( @pcParametername = "key") String key, @RpcParameter(name = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @RpcOptional String filename)60 public Object prefGetValue( 61 @RpcParameter(name = "key") String key, 62 @RpcParameter(name = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @RpcOptional String filename) { 63 SharedPreferences p = getPref(filename); 64 return p.getAll().get(key); 65 } 66 67 @Rpc(description = "Write a value to shared preferences") prefPutValue( @pcParametername = "key") String key, @RpcParameter(name = "value") Object value, @RpcParameter(name = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @RpcOptional String filename)68 public void prefPutValue( 69 @RpcParameter(name = "key") String key, 70 @RpcParameter(name = "value") Object value, 71 @RpcParameter(name = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @RpcOptional String filename) 72 throws IOException { 73 if (filename == null || filename.equals("")) { 74 throw new IOException("Can't write to default preferences."); 75 } 76 SharedPreferences p = getPref(filename); 77 Editor e = p.edit(); 78 if (value instanceof Boolean) { 79 e.putBoolean(key, (Boolean) value); 80 } else if (value instanceof Long) { 81 e.putLong(key, (Long) value); 82 } else if (value instanceof Integer) { 83 e.putLong(key, (Integer) value); 84 } else if (value instanceof Float) { 85 e.putFloat(key, (Float) value); 86 } else if (value instanceof Double) { // TODO: Not sure if this is a good idea 87 e.putFloat(key, ((Double) value).floatValue()); 88 } else { 89 e.putString(key, value.toString()); 90 } 91 e.commit(); 92 } 93 94 @Rpc(description = "Get list of Shared Preference Values", returns = "Map of key,value") prefGetAll( @pcParametername = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @pcOptional String filename)95 public Map<String, ?> prefGetAll( 96 @RpcParameter(name = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @RpcOptional String filename) { 97 return getPref(filename).getAll(); 98 } 99 getPref(String filename)100 private SharedPreferences getPref(String filename) { 101 if (filename == null || filename.equals("")) { 102 return PreferenceManager.getDefaultSharedPreferences(mService); 103 } 104 return mService.getSharedPreferences(filename, 0); 105 106 } 107 108 @Override shutdown()109 public void shutdown() { 110 111 } 112 } 113