All Downloads are FREE. Search and download functionalities are using the official Maven repository.

playn.android.AndroidStorage Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
/**
 * Copyright 2010 The PlayN Authors
 *
 * 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 playn.android;

import java.util.ArrayList;
import java.util.Map;

import android.app.Activity;
import android.content.SharedPreferences;

import playn.core.BatchImpl;
import playn.core.Storage;

public class AndroidStorage implements Storage {

  private static final String PREFS_NAME = "playn";
  private final AndroidPlatform platform;
  private SharedPreferences settings;

  public AndroidStorage(AndroidPlatform platform) {
    this.platform = platform;
    this.settings = platform.activity.getSharedPreferences(PREFS_NAME, 0);
  }

  @Override
  public void setItem(String key, String data) throws RuntimeException {
    settings.edit().putString(key, data).commit();
  }

  @Override
  public void removeItem(String key) {
    settings.edit().remove(key).commit();
  }

  @Override
  public String getItem(String key) {
    return settings.getString(key, null);
  }

  @Override
  public Batch startBatch() {
    return new BatchImpl(this) {
      private SharedPreferences.Editor edit;
      protected void onBeforeCommit() {
        edit = settings.edit();
      }
      protected void setImpl(String key, String data) {
        edit.putString(key, data);
      }
      protected void removeImpl(String key, String data) {
        edit.remove(key);
      }
      protected void onAfterCommit() {
        edit.commit();
        edit = null;
      }
    };
  }

  @Override
  public Iterable keys() {
    return new ArrayList(settings.getAll().keySet());
  }

  @Override
  public boolean isPersisted() {
    return true;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy