Example usage for android.content SharedPreferences getBoolean

List of usage examples for android.content SharedPreferences getBoolean

Introduction

In this page you can find the example usage for android.content SharedPreferences getBoolean.

Prototype

boolean getBoolean(String key, boolean defValue);

Source Link

Document

Retrieve a boolean value from the preferences.

Usage

From source file:Main.java

public static Object get(Context context, String key, Object defaultObject) {
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    if (defaultObject instanceof String) {
        return sp.getString(key, (String) defaultObject);
    } else if (defaultObject instanceof Integer) {
        return sp.getInt(key, (Integer) defaultObject);
    } else if (defaultObject instanceof Boolean) {
        return sp.getBoolean(key, (Boolean) defaultObject);
    } else if (defaultObject instanceof Float) {
        return sp.getFloat(key, (Float) defaultObject);
    } else if (defaultObject instanceof Long) {
        return sp.getLong(key, (Long) defaultObject);
    }/*w  ww.  j  a v  a 2s  . c  om*/
    return null;
}

From source file:Main.java

public static Object get(Context context, String key, Object defaultObject) {
    SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);

    if (defaultObject instanceof String) {
        return sp.getString(key, (String) defaultObject);
    } else if (defaultObject instanceof Integer) {
        return sp.getInt(key, (Integer) defaultObject);
    } else if (defaultObject instanceof Boolean) {
        return sp.getBoolean(key, (Boolean) defaultObject);
    } else if (defaultObject instanceof Float) {
        return sp.getFloat(key, (Float) defaultObject);
    } else if (defaultObject instanceof Long) {
        return sp.getLong(key, (Long) defaultObject);
    }/*from   w  ww . j a  va  2  s .  c o m*/

    return null;
}

From source file:com.cmgapps.android.util.CMGAppRater.java

private static String ratePreferenceToString(SharedPreferences pref) {
    JSONObject thiz = new JSONObject();
    try {// w  w  w.  j  a  v a2s .co m
        thiz.put(DECLINED_RATE, pref.getBoolean(DECLINED_RATE, false));
        thiz.put(APP_RATED, pref.getBoolean(APP_RATED, false));
        thiz.put(TRACKING_VERSION, pref.getInt(TRACKING_VERSION, -1));
        thiz.put(FIRST_USE,
                SimpleDateFormat.getDateTimeInstance().format(new Date(pref.getLong(FIRST_USE, 0L))));
        thiz.put(USE_COUNT, pref.getInt(USE_COUNT, 0));
        thiz.put(REMIND_LATER_DATE,
                SimpleDateFormat.getDateTimeInstance().format(new Date(pref.getLong(REMIND_LATER_DATE, 0L))));
    } catch (JSONException exc) {
        LOGE(TAG, "Error creating JSON Object", exc);
    }

    return thiz.toString();
}

From source file:ch.fixme.status.Widget.java

protected static void updateWidget(final Context ctxt, int widgetId, AppWidgetManager manager, Bitmap bitmap,
        String text) {/*from  w  w w .  j av  a  2  s. c om*/
    RemoteViews views = new RemoteViews(ctxt.getPackageName(), R.layout.widget);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctxt);
    Editor edit = prefs.edit();
    if (prefs.getBoolean(Prefs.KEY_WIDGET_TRANSPARENCY, Prefs.DEFAULT_WIDGET_TRANSPARENCY)) {
        views.setInt(R.id.widget_image, "setBackgroundResource", 0);
    } else {
        views.setInt(R.id.widget_image, "setBackgroundResource", android.R.drawable.btn_default_small);
    }
    if (bitmap != null) {
        views.setImageViewBitmap(R.id.widget_image, bitmap);
        edit.putBoolean(Main.PREF_FORCE_WIDGET + widgetId, false); // Don't
        // need
        // to
        // force
    } else {
        views.setImageViewResource(R.id.widget_image, android.R.drawable.ic_popup_sync);
        edit.putBoolean(Main.PREF_FORCE_WIDGET + widgetId, true); // Something
        // went
        // wrong
    }
    if (text != null) {
        views.setTextViewText(R.id.widget_status, text);
        views.setViewVisibility(R.id.widget_status, View.VISIBLE);
    } else {
        views.setViewVisibility(R.id.widget_status, View.GONE);
    }
    Intent clickIntent = new Intent(ctxt, Main.class);
    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctxt, widgetId, clickIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
    manager.updateAppWidget(widgetId, views);
    // Is initialized
    edit.putBoolean(Main.PREF_INIT_WIDGET + widgetId, true);
    edit.commit();
}

From source file:net.shirayu.android.WlanLogin.MyHttpClient.java

public static KeyStore loadKeyStore(Context context) throws KeyStoreException, NoSuchAlgorithmException,
        CertificateException, FileNotFoundException, IOException, NoSuchProviderException {
    /*/*from   www. j  ava 2s  . c o m*/
     * Original implementation.
    I refer the following sites about the default keystore.
    http://wiki.livedoor.jp/syo1976/d/javassl
    http://d.hatena.ne.jp/Kazzz/20110319/p1
     */
    KeyStore certstore;
    if (Integer.valueOf(Build.VERSION.SDK) >= 14) {
        // load from unified key store
        certstore = KeyStore.getInstance("AndroidCAStore");
        certstore.load(null, null);
    } else {
        certstore = KeyStore.getInstance(KeyStore.getDefaultType());
        certstore.load(new FileInputStream(System.getProperty("javax.net.ssl.trustStore")), null); //load default keystore
    }

    //load self_signed_certificate?
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    final Boolean use_self_signed_certificate = sharedPreferences.getBoolean("use_self_signed_certificate",
            false);
    if (use_self_signed_certificate) {
        final Boolean use_folder = sharedPreferences.getBoolean("use_self_signed_certificate_folder", false);
        final String filename = sharedPreferences.getString("self_signed_certificate_path", "//");
        File myfile = new File(filename);

        if (use_folder) {
            for (File file : new File(myfile.getParent()).listFiles()) {
                if (file.isDirectory())
                    continue;
                FileInputStream stream = new FileInputStream(file);
                X509Certificate cert = MyHttpClient.readPem(stream);
                certstore.setCertificateEntry(file.getName(), cert);
            }
        } else {
            FileInputStream stream = new FileInputStream(myfile);
            X509Certificate cert = MyHttpClient.readPem(stream);
            certstore.setCertificateEntry(myfile.getName(), cert);
        }
        ;
    }
    ;

    return certstore;
}

From source file:com.hoccer.api.android.AsyncLinccer.java

public static boolean getFlagFromSharedPreferences(Context context, String prefid, boolean defaultValue) {
    SharedPreferences prefs = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);

    boolean flag = prefs.getBoolean(prefid, defaultValue);
    return flag;//from w w  w  .j a v a 2s.  c  o m
}

From source file:com.github.chenxiaolong.dualbootpatcher.switcher.SwitcherUtils.java

public static void reboot(final Context context) {
    SharedPreferences prefs = context.getSharedPreferences("settings", 0);
    final boolean confirm = prefs.getBoolean("confirm_reboot", false);

    new Thread() {
        @Override//from   ww w  . j av a  2s .  co  m
        public void run() {
            MbtoolConnection conn = null;
            try {
                conn = new MbtoolConnection(context);
                MbtoolInterface iface = conn.getInterface();

                iface.rebootViaFramework(confirm);
            } catch (Exception e) {
                Log.e(TAG, "Failed to reboot via framework", e);
            } finally {
                IOUtils.closeQuietly(conn);
            }
        }
    }.start();
}

From source file:com.teinproductions.tein.papyrosprogress.UpdateCheckReceiver.java

private static void issueNotification(Context context, Set<String> addedMilestones,
        Set<String> removedMilestones, Map<String, int[]> changedProgresses) {
    String title = context.getString(R.string.notification_title);
    StringBuilder message = new StringBuilder();

    // Changed progresses
    for (String milestoneTitle : changedProgresses.keySet()) {
        int oldProgress = changedProgresses.get(milestoneTitle)[0];
        int newProgress = changedProgresses.get(milestoneTitle)[1];
        message.append("\n").append(String.format(context.getString(R.string.notific_msg_text_format),
                milestoneTitle, oldProgress, newProgress));
    }//  w w  w  . j a  v a 2 s . c  o m

    // Added milestones
    for (String milestoneTitle : addedMilestones) {
        message.append("\n").append(context.getString(R.string.milestone_added_notification, milestoneTitle));
    }

    // Removed milestones
    for (String milestoneTitle : removedMilestones) {
        message.append("\n").append(context.getString(R.string.milestone_removed_notification, milestoneTitle));
    }

    // Remove first newline
    message.delete(0, 1);

    // Create PendingIntent
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
    boolean sound = pref.getBoolean(Constants.NOTIFICATION_SOUND_PREF, true);
    boolean vibrate = pref.getBoolean(Constants.NOTIFICATION_VIBRATE_PREF, true);
    boolean light = pref.getBoolean(Constants.NOTIFICATION_LIGHT_PREF, true);
    int defaults = 0;
    if (sound)
        defaults = defaults | Notification.DEFAULT_SOUND;
    if (vibrate)
        defaults = defaults | Notification.DEFAULT_VIBRATE;
    if (light)
        defaults = defaults | Notification.DEFAULT_LIGHTS;

    // Build the notification
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentTitle(title)
            .setContentText(message).setContentIntent(pendingIntent)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setSmallIcon(R.mipmap.notification_small_icon)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
            .setDefaults(defaults).setAutoCancel(true);

    // Issue the notification
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(PROGRESS_NOTIFICATION_ID, builder.build());
}

From source file:com.blork.anpod.util.BitmapUtils.java

public static BitmapResult fetchImage(Context context, Picture picture, int desiredWidth, int desiredHeight) {

    // First compute the cache key and cache file path for this URL
    File cacheFile = null;//from   ww w  .ja  va 2  s. co  m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        Log.d("APOD", "creating cache file");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

        if (prefs.getBoolean("archive", false)) {
            cacheFile = new File(Environment.getExternalStorageDirectory() + File.separator + "APOD"
                    + File.separator + toSlug(picture.title) + ".jpg");
        } else {
            cacheFile = new File(Environment.getExternalStorageDirectory() + File.separator + "Android"
                    + File.separator + "data" + File.separator + "com.blork.anpod" + File.separator + "cache"
                    + File.separator + toSlug(picture.title) + ".jpg");
        }
    } else {
        Log.d("APOD", "SD card not mounted");
        Log.d("APOD", "creating cache file");
        cacheFile = new File(context.getCacheDir() + File.separator + toSlug(picture.title) + ".jpg");
    }

    if (cacheFile != null && cacheFile.exists()) {
        Log.d("APOD", "Cache file exists, using it.");
        try {
            Bitmap bitmap = decodeStream(new FileInputStream(cacheFile), desiredWidth, desiredHeight);
            return new BitmapResult(bitmap, Uri.fromFile(cacheFile));
        } catch (FileNotFoundException e) {
        }
    }

    try {
        Log.d("APOD", "Not cached, fetching");
        BitmapUtils.manageCache(toSlug(picture.title), context);
        // TODO: check for HTTP caching headers
        final HttpClient httpClient = SyncUtils.getHttpClient(context.getApplicationContext());
        final HttpResponse resp = httpClient.execute(new HttpGet(picture.getFullSizeImageUrl()));
        final HttpEntity entity = resp.getEntity();

        final int statusCode = resp.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK || entity == null) {
            return null;
        }

        final byte[] respBytes = EntityUtils.toByteArray(entity);

        Log.d("APOD", "Writing cache file " + cacheFile.getName());
        try {
            cacheFile.getParentFile().mkdirs();
            cacheFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(cacheFile);
            fos.write(respBytes);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d("APOD", "Error writing to bitmap cache: " + cacheFile.toString(), e);
        } catch (IOException e) {
            Log.d("APOD", "Error writing to bitmap cache: " + cacheFile.toString(), e);
        }

        // Decode the bytes and return the bitmap.
        Log.d("APOD", "Reiszing bitmap image");

        Bitmap bitmap = decodeStream(new ByteArrayInputStream(respBytes), desiredWidth, desiredHeight);
        Log.d("APOD", "Returning bitmap image");
        return new BitmapResult(bitmap, Uri.fromFile(cacheFile));
    } catch (Exception e) {
        Log.d("APOD", "Problem while loading image: " + e.toString(), e);
    }

    return null;
}

From source file:com.libresoft.apps.ARviewer.Utils.GeoNames.AltitudeManager.java

public static double getAbsoluteAltitude(Context context, double base_altitude, boolean is_floor) {
    double altitude = AltitudeManager.NO_ALTITUDE_VALUE;

    if (base_altitude != AltitudeManager.NO_ALTITUDE_VALUE) {
        try {/*from  w  ww . j ava 2s  . co  m*/
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

            float user_height = ((float) sharedPreferences.getInt(AltitudePreferences.KEY_USER_HEIGHT, 175))
                    / 100;

            if (is_floor && sharedPreferences.getBoolean(AltitudePreferences.KEY_USE_FLOOR, false)) {
                int floor_number = sharedPreferences.getInt(AltitudePreferences.KEY_FLOOR, 0);

                altitude = (float) (base_altitude + user_height + floor_number * 3); // 3 meters per room
                Log.d("AltitudeManager", "Using floor");
            } else
                altitude = (float) (base_altitude + user_height);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e("AltitudeManager", e.toString());
        }
    }

    return altitude;
}