Example usage for android.app Activity getPreferences

List of usage examples for android.app Activity getPreferences

Introduction

In this page you can find the example usage for android.app Activity getPreferences.

Prototype

public SharedPreferences getPreferences(@Context.PreferencesMode int mode) 

Source Link

Document

Retrieve a SharedPreferences object for accessing preferences that are private to this activity.

Usage

From source file:dynamite.zafroshops.app.fragment.TypedZopsFragment.java

private void setZops(final boolean force) {
    Activity activity = getActivity();
    adapter = new TypedZopListViewAdapter(getActivity(), R.id.listViewItem, typedZops);
    final SharedPreferences preferences = activity.getPreferences(0);

    final SharedPreferences.Editor editor = preferences.edit();
    final ZopType type = (ZopType) getArguments().get(ARG_ZOP_TYPE);
    final String key = StorageKeys.ZOPS_KEY + type.toString();

    zopType = type;/*www . j  av  a2s  . c  o m*/

    if (!preferences.contains(key)) {
        InputStream is = getResources().openRawResource(R.raw.zops);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        typedZops = new ArrayList<>(Collections2.filter(
                (ArrayList<MobileZop>) new Gson().fromJson(reader, new TypeToken<ArrayList<MobileZop>>() {
                }.getType()), new Predicate<MobileZop>() {
                    @Override
                    public boolean apply(MobileZop input) {
                        return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                                || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                                || input.CountryID.equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                    }
                }));

        try {
            FileOutputStream fos = activity.openFileOutput(key, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            int count = typedZops.size();

            oos.writeObject(typedZops);
            oos.close();
            fos.close();
            editor.putString(key, Integer.toString(count));
            editor.commit();
            setCount(count);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    else if (preferences.contains(key)) {
        try {
            FileInputStream fis = activity.openFileInput(key);
            ObjectInputStream ois = new ObjectInputStream(fis);

            ArrayList<MobileZop> temp = (ArrayList) ois.readObject();
            typedZops = new ArrayList<>(Collections2.filter(temp, new Predicate<MobileZop>() {
                @Override
                public boolean apply(MobileZop input) {
                    return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                            || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                            || input.CountryID.equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                }
            }));
            ois.close();
            fis.close();
            setCount(typedZops.size());
            if (adapter != null) {
                adapter.setObjects(typedZops);
                adapter.notifyDataSetChanged();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    if (force) {
        final ArrayList<Pair<String, String>> parameters;
        if (MainActivity.LastLocation != null) {
            parameters = new ArrayList<Pair<String, String>>() {
                {
                    add(new Pair<>("latitude", Double.toString(MainActivity.LastLocation.Latitude)));
                    add(new Pair<>("longitude", Double.toString(MainActivity.LastLocation.Longitude)));
                    add(new Pair<>("type", type.getText()));
                }
            };
        } else {
            parameters = new ArrayList<Pair<String, String>>() {
                {
                    add(new Pair<>("type", type.getText()));
                }
            };
        }

        ListenableFuture<JsonElement> result = MainActivity.MobileClient.invokeApi("mobileZop", "GET",
                parameters);

        Futures.addCallback(result, new FutureCallback<JsonElement>() {
            Activity activity = getActivity();

            @Override
            public void onSuccess(JsonElement result) {
                JsonArray typesAsJson = result.getAsJsonArray();
                if (typesAsJson != null) {
                    ArrayList<MobileZop> temp = new Gson().fromJson(result,
                            new TypeToken<ArrayList<MobileZop>>() {
                            }.getType());

                    int max = Collections.max(temp, new Comparator<MobileZop>() {
                        @Override
                        public int compare(MobileZop lhs, MobileZop rhs) {
                            if (lhs.DataVersion < rhs.DataVersion) {
                                return -1;
                            } else if (lhs.DataVersion > rhs.DataVersion) {
                                return 1;
                            } else {
                                return 0;
                            }
                        }
                    }).DataVersion;
                    ((MainActivity) activity).Versions.put(zopType, max);

                    try {
                        FileOutputStream fos = activity.openFileOutput(key, Context.MODE_PRIVATE);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);
                        int count = typedZops.size();

                        typedZops = new ArrayList<>(Collections2.filter(temp, new Predicate<MobileZop>() {
                            @Override
                            public boolean apply(MobileZop input) {
                                return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                                        || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                                        || input.CountryID
                                                .equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                            }
                        }));
                        oos.writeObject(typedZops);
                        oos.close();
                        fos.close();
                        editor.putString(key, Integer.toString(count));
                        editor.commit();
                        setCount(count);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (adapter != null) {
                    adapter.setObjects(typedZops);
                    resetVisibility(false);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(@NonNull Throwable t) {
                ListView zops = (ListView) activity.findViewById(R.id.listViewZops);
                RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                LinearLayout noZops = (LinearLayout) activity.findViewById(R.id.noZops);

                resetVisibility(zops, noZops, loader, false);
            }
        });
    }
}

From source file:com.gmail.walles.johan.batterylogger.BatteryPlotFragment.java

private void showAlertDialogOnce(String title, String message) {
    // Don't show more than one dialog at a time
    if (visibleDialog != null && visibleDialog.isShowing()) {
        return;/* w ww  .j  a v a  2s  .c om*/
    }

    final String shownTag = title + ": " + message + " shown";
    if (shownDialogs.contains(shownTag)) {
        return;
    }

    final Activity activity = getActivity();
    if (activity == null) {
        return;
    }
    final SharedPreferences preferences = activity.getPreferences(Context.MODE_PRIVATE);
    if (preferences.getBoolean(shownTag, false)) {
        shownDialogs.add(shownTag);
        return;
    }

    visibleDialog = showAlertDialog(title, message, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            preferences.edit().putBoolean(shownTag, true).commit();
            shownDialogs.add(shownTag);
            dialogInterface.dismiss();
        }
    });
}

From source file:com.serenegiant.aceparrot.BaseFragment.java

protected LayoutInflater getThemedLayoutInflater(final LayoutInflater inflater) {
    final Activity context = getActivity();
    final SharedPreferences pref = context.getPreferences(0);
    final int layout_style = getLayoutStyle(pref.getInt(AppConst.KEY_ICON_TYPE, 0));
    // create ContextThemeWrapper from the original Activity Context with the custom theme
    final Context contextThemeWrapper = new ContextThemeWrapper(context, layout_style);
    // clone the inflater using the ContextThemeWrapper
    return inflater.cloneInContext(contextThemeWrapper);
}

From source file:com.serenegiant.aceparrot.ConfigFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (DEBUG)//from w  w  w . j  a  v  a  2 s . c  o  m
        Log.v(TAG, "onAttach:");
    mMaxAltitudeFormat = getString(R.string.config_max_altitude);
    mMaxTiltFormat = getString(R.string.config_max_tilt);
    mMaxVerticalSpeedFormat = getString(R.string.config_max_vertical_speed);
    mMaxRotationSpeedFormat = getString(R.string.config_max_rotating_speed);

    mGamepadScaleXFormat = getString(R.string.config_scale_x);
    mGamepadScaleYFormat = getString(R.string.config_scale_y);
    mGamepadScaleZFormat = getString(R.string.config_scale_z);
    mGamepadScaleRFormat = getString(R.string.config_scale_r);
    mGamepadSensitivityFormat = getString(R.string.config_control_max_gamepad);

    mAutopilotScaleXFormat = getString(R.string.config_scale_x);
    mAutopilotScaleYFormat = getString(R.string.config_scale_y);
    mAutopilotScaleZFormat = getString(R.string.config_scale_z);
    mAutopilotScaleRFormat = getString(R.string.config_scale_r);
    mAutopilotMaxControlValueFormat = getString(R.string.config_control_max);

    mPref = activity.getPreferences(0);
}