Android Open Source - screenstandby Backup Helper






From Project

Back to project page screenstandby.

License

The source code is released under:

GNU General Public License

If you think the Android project screenstandby listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (c) 2014 Hoang Nguyen.//from w  w  w .  j  ava2s  .  c  o m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Hoang Nguyen - initial API and implementation
 ******************************************************************************/
package com.nkahoang.screenstandby;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import java.util.zip.GZIPOutputStream;
import java.util.zip.GZIPInputStream;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.preference.PreferenceManager;

class BackupHelper {
  static String GenerateFilename()
  {
    String logfile = Environment.getExternalStorageDirectory().getPath() + "/screenstandby"+getDateTime()+".backup-ss";
    return logfile;
  }
  
  final static String getDateTime()  
  {  
      SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-hhmmss");
      return df.format(Calendar.getInstance().getTime());  
  }
  
  static String saveSharedPreferencesToFile(Context context)
  {
    try
    {
      File myFile = new File(GenerateFilename());
      myFile.createNewFile();
      return saveSharedPreferencesToFile(context,myFile);
    }
    catch(Exception e)
    {
      Logger.Log(context, e);
      return null;
    }
  }
  static String saveSharedPreferencesToFile(Context context, File dst) {
      String res = null;
      ObjectOutputStream output = null;
      try {
        GZIPOutputStream outputGZIP = new GZIPOutputStream(new FileOutputStream(dst));
          output = new ObjectOutputStream(outputGZIP);
          SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
          Map<String, Object> shallowCopy = new HashMap<String, Object>(pref.getAll());
          shallowCopy.remove("extendedlog");
          output.writeObject(shallowCopy); //write everything but not the log
          res = dst.getAbsolutePath();
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }finally {
          try {
              if (output != null) {
                  output.flush();
                  output.close();
              }
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
      return res;
  }

  @SuppressWarnings("unchecked")
  static boolean loadSharedPreferencesFromFile(Context context, File src) {
      boolean res = false;
      ObjectInputStream input = null;
      try {
        GZIPInputStream inputGZIP = new GZIPInputStream(new FileInputStream(src));
          input = new ObjectInputStream(inputGZIP);
              Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context).edit();
              prefEdit.clear();
              Map<String, Object> entries = (Map<String, Object>) input.readObject();
              for (Entry<String, ?> entry : entries.entrySet()) {
                  Object v = entry.getValue();
                  String key = entry.getKey();

                  if (v instanceof Boolean)
                      prefEdit.putBoolean(key, ((Boolean) v).booleanValue());
                  else if (v instanceof Float)
                      prefEdit.putFloat(key, ((Float) v).floatValue());
                  else if (v instanceof Integer)
                      prefEdit.putInt(key, ((Integer) v).intValue());
                  else if (v instanceof Long)
                      prefEdit.putLong(key, ((Long) v).longValue());
                  else if (v instanceof String)
                      prefEdit.putString(key, ((String) v));
              }
              prefEdit.commit();
          res = true;         
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }finally {
          try {
              if (input != null) {
                  input.close();
              }
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      }
      return res;
  }
}




Java Source Code List

android.os.UEventObserver.java
com.nkahoang.kernelswitchobserver.DockObserver.java
com.nkahoang.kernelswitchobserver.GenericHardwareObserver.java
com.nkahoang.kernelswitchobserver.HDMIObserver.java
com.nkahoang.kernelswitchobserver.HardwareNotFoundException.java
com.nkahoang.kernelswitchobserver.HeadsetObserver.java
com.nkahoang.kernelswitchobserver.UEventStateChangeHandler.java
com.nkahoang.screenstandby.AppDetector.java
com.nkahoang.screenstandby.AutoSettingWizard.java
com.nkahoang.screenstandby.BTReceiver.java
com.nkahoang.screenstandby.BackupHelper.java
com.nkahoang.screenstandby.BaseActivity.java
com.nkahoang.screenstandby.BootTimeReceiver.java
com.nkahoang.screenstandby.ChangeLogHandler.java
com.nkahoang.screenstandby.DisableActivity.java
com.nkahoang.screenstandby.DockReceiver.java
com.nkahoang.screenstandby.EnableActivity.java
com.nkahoang.screenstandby.ExceptionHandler.java
com.nkahoang.screenstandby.FlowLayout.java
com.nkahoang.screenstandby.FontManager.java
com.nkahoang.screenstandby.HDMIReceiver.java
com.nkahoang.screenstandby.HUDNative.java
com.nkahoang.screenstandby.HeadsetReceiver.java
com.nkahoang.screenstandby.IconContextMenu.java
com.nkahoang.screenstandby.Logger.java
com.nkahoang.screenstandby.Main.java
com.nkahoang.screenstandby.ManualBrightnessChangerActivity.java
com.nkahoang.screenstandby.MediaButtonControlReceiver.java
com.nkahoang.screenstandby.NotificationBar.java
com.nkahoang.screenstandby.NumberPickerButton.java
com.nkahoang.screenstandby.NumberPicker.java
com.nkahoang.screenstandby.OneTwoDimmer.java
com.nkahoang.screenstandby.OutcomingCallReceiver.java
com.nkahoang.screenstandby.PreferenceListFragment.java
com.nkahoang.screenstandby.RemoteControlSelectActivity.java
com.nkahoang.screenstandby.RemoteControllerActivity.java
com.nkahoang.screenstandby.RemoteServerActivity.java
com.nkahoang.screenstandby.ServiceToggleReceiver.java
com.nkahoang.screenstandby.SettingActivity.java
com.nkahoang.screenstandby.ShortcutActivity.java
com.nkahoang.screenstandby.ShortcutOnActivity.java
com.nkahoang.screenstandby.StandbyService.java
com.nkahoang.screenstandby.StandbyToggleReceiver.java
com.nkahoang.screenstandby.StandbyWidgetProvider2by2.java
com.nkahoang.screenstandby.StandbyWidgetProvider.java
com.nkahoang.screenstandby.TimePickerDialog.java
com.nkahoang.screenstandby.TimePicker.java
com.nkahoang.screenstandby.TroubleshootingActivity.java
com.nkahoang.screenstandby.VolumeKeyDetector.java
com.nkahoang.screenstandby.XTopDPIChanger.java
com.nkahoang.screenstandby.remotecontrol.ClientService.java
com.nkahoang.screenstandby.remotecontrol.Core.java
com.nkahoang.screenstandby.remotecontrol.OnClientStateChangedHandler.java
com.nkahoang.screenstandby.remotecontrol.OnServiceAddressResolvedHandler.java
com.nkahoang.screenstandby.remotecontrol.OnStateChangedHandler.java
com.nkahoang.screenstandby.remotecontrol.RemoteAppPackages.java
com.nkahoang.screenstandby.remotecontrol.RemotePackageInfo.java
com.nkahoang.screenstandby.remotecontrol.ServerService.java
com.nkahoang.screenstandby.remotecontrol.ServiceBinder.java
com.nkahoang.screenstandby.settings.DeviceSettings.java
com.nkahoang.screenstandby.settings.RootChecker.java
com.nkahoang.screenstandby.settings.UpdateChecker.java