Home | History | Annotate | Download | only in facade
      1 /*
      2  * Copyright (C) 2017 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.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  */
     48 
     49 public class PreferencesFacade extends RpcReceiver {
     50 
     51   private Service mService;
     52 
     53   public PreferencesFacade(FacadeManager manager) {
     54     super(manager);
     55     mService = manager.getService();
     56   }
     57 
     58   @Rpc(description = "Read a value from shared preferences")
     59   public Object prefGetValue(
     60       @RpcParameter(name = "key") String key,
     61       @RpcParameter(name = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @RpcOptional String filename) {
     62     SharedPreferences p = getPref(filename);
     63     return p.getAll().get(key);
     64   }
     65 
     66   @Rpc(description = "Write a value to shared preferences")
     67   public void prefPutValue(
     68       @RpcParameter(name = "key") String key,
     69       @RpcParameter(name = "value") Object value,
     70       @RpcParameter(name = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @RpcOptional String filename)
     71       throws IOException {
     72     if (filename == null || filename.equals("")) {
     73       throw new IOException("Can't write to default preferences.");
     74     }
     75     SharedPreferences p = getPref(filename);
     76     Editor e = p.edit();
     77     if (value instanceof Boolean) {
     78       e.putBoolean(key, (Boolean) value);
     79     } else if (value instanceof Long) {
     80       e.putLong(key, (Long) value);
     81     } else if (value instanceof Integer) {
     82       e.putLong(key, (Integer) value);
     83     } else if (value instanceof Float) {
     84       e.putFloat(key, (Float) value);
     85     } else if (value instanceof Double) { // TODO: Not sure if this is a good idea
     86       e.putFloat(key, ((Double) value).floatValue());
     87     } else {
     88       e.putString(key, value.toString());
     89     }
     90     e.commit();
     91   }
     92 
     93   @Rpc(description = "Get list of Shared Preference Values", returns = "Map of key,value")
     94   public Map<String, ?> prefGetAll(
     95       @RpcParameter(name = "filename", description = "Desired preferences file. If not defined, uses the default Shared Preferences.") @RpcOptional String filename) {
     96     return getPref(filename).getAll();
     97   }
     98 
     99   private SharedPreferences getPref(String filename) {
    100     if (filename == null || filename.equals("")) {
    101       return PreferenceManager.getDefaultSharedPreferences(mService);
    102     }
    103     return mService.getSharedPreferences(filename, 0);
    104 
    105   }
    106 
    107   @Override
    108   public void shutdown() {
    109 
    110   }
    111 }
    112