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:it.scoppelletti.mobilepower.app.HelpDialogFragment.java

/**
 * Verifica se un’attività ha già visualizzato una
 * Guida.//from ww  w . j  a v a  2  s .  c om
 * 
 * @param  activity Attività.
 * @param  prefKey  Chiave della preferenza che indica se la Guida è
 *                  già stata visualizzata.
 * @return          Esito della verifica.
 */
public static boolean isShown(Activity activity, String prefKey) {
    SharedPreferences prefs;

    if (activity == null) {
        throw new NullPointerException("Argument activity is null.");
    }
    if (StringUtils.isBlank(prefKey)) {
        throw new NullPointerException("Argument prefKey is null.");
    }

    prefs = activity.getPreferences(Context.MODE_PRIVATE);
    return prefs.getBoolean(prefKey, false);
}

From source file:com.medlog.medlogmobile.NewMessageNotification.java

public static AlertDialog.Builder eulaAlert(final Activity mActivity) {
    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity).setTitle(" End-User License Agreement")
            .setMessage(EULATEEXT).setPositiveButton(android.R.string.ok, new Dialog.OnClickListener() {

                @Override/*w  w  w  . j av a 2 s. c o  m*/
                public void onClick(DialogInterface dialogInterface, int i) {
                    // Mark this version as read.
                    SharedPreferences sp = mActivity.getPreferences(Context.MODE_PRIVATE);

                    SharedPreferences.Editor editor = sp.edit();
                    editor.putBoolean(mActivity.getString(R.string.PREF_EULA), true);
                    editor.commit();
                    dialogInterface.dismiss();
                }
            }).setNegativeButton(android.R.string.cancel, new Dialog.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Close the activity as they have declined the EULA

                    mActivity.finishAffinity();
                    System.exit(0);
                }

            });
    return builder;
}

From source file:me.qisthi.cuit.helper.StorageHelper.java

public static void writePreferenceStatusValue(Activity activity, String key, List<String[]> statuses) {
    String rawStatuses = "";
    ObjectMapper mapper = new ObjectMapper();
    //        for(Status  status : statuses)
    //        {/*from  ww w.  j  a  va 2s .  com*/
    //            String rawStatus = null;
    //            try {
    //                rawStatus = mapper.writeValueAsString(status);
    //            } catch (JsonProcessingException e) {
    //                e.printStackTrace();
    //            }
    //            rawStatuses.add(rawStatus);
    //        }
    try {
        rawStatuses = mapper.writeValueAsString(statuses);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    SharedPreferences sharedPreferences = activity.getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString(key, rawStatuses);
    editor.apply();
}

From source file:org.uab.deic.uabdroid.solutions.unit5.FormFragment.java

@Override
public void onActivityCreated(Bundle _savedInstanceState) {
    super.onActivityCreated(_savedInstanceState);

    final Activity parentActivity = getActivity();

    SharedPreferences activityPreferences = parentActivity.getPreferences(Context.MODE_PRIVATE);

    mEditTextName = (EditText) parentActivity.findViewById(R.id.edittext_form_name);
    mEditTextDeveloper = (EditText) parentActivity.findViewById(R.id.edittext_form_developer);
    mDatePickerDate = (DatePicker) parentActivity.findViewById(R.id.datepicker_form_date);
    mEditTextURL = (EditText) parentActivity.findViewById(R.id.edittext_form_url);

    if (activityPreferences.getBoolean(FormAppActivity.STATE_NOT_SAVED, false)) {
        mEditTextName.setText(activityPreferences.getString(FormAppActivity.FORM_FIELD_NAME, ""));

        mEditTextDeveloper.setText(activityPreferences.getString(FormAppActivity.FORM_FIELD_DEVELOPER, ""));

        Calendar calendar = Calendar.getInstance();
        int day = activityPreferences.getInt(FormAppActivity.FORM_FIELD_DAY,
                calendar.get(Calendar.DAY_OF_MONTH));
        int month = activityPreferences.getInt(FormAppActivity.FORM_FIELD_MONTH,
                calendar.get(Calendar.MONTH) + 1);
        int year = activityPreferences.getInt(FormAppActivity.FORM_FIELD_YEAR, calendar.get(Calendar.YEAR));

        mDatePickerDate.updateDate(year, month, day);

        mEditTextURL.setText(activityPreferences.getString(FormAppActivity.FORM_FIELD_URL, ""));

        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putBoolean(FormAppActivity.STATE_NOT_SAVED, false);
        editor.commit();/*  w w w.  j  ava 2s  .  c  o  m*/
    }

    Button cleanButton = (Button) parentActivity.findViewById(R.id.button_form_clean);
    cleanButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mEditTextName.setText("");
            mEditTextDeveloper.setText("");
            Calendar calendar = Calendar.getInstance();
            mDatePickerDate.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                    calendar.get(Calendar.DAY_OF_MONTH));
            mEditTextURL.setText("");
        }
    });

    Button acceptButton = (Button) parentActivity.findViewById(R.id.button_form_accept);
    acceptButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, mDatePickerDate.getDayOfMonth());
            calendar.set(Calendar.MONTH, mDatePickerDate.getMonth() - 1);
            calendar.set(Calendar.YEAR, mDatePickerDate.getYear());

            String name = mEditTextName.getText().toString();
            String developer = mEditTextDeveloper.getText().toString();
            String url = mEditTextURL.getText().toString();

            DatabaseAdapter databaseAdapter = new DatabaseAdapter(parentActivity);
            databaseAdapter.open();
            databaseAdapter.insertApp(name, developer, calendar, url);
            databaseAdapter.close();

            ((FormAppActivity) parentActivity).setButtonPressed();

            parentActivity.finish();
        }
    });

    Button cancelButton = (Button) parentActivity.findViewById(R.id.button_form_cancel);
    cancelButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            ((FormAppActivity) parentActivity).setButtonPressed();
            parentActivity.finish();
        }
    });
}

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

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mPref = activity.getPreferences(0);
}

From source file:org.uab.deic.uabdroid.solutions.unit3.FormFragment.java

@Override
public void onActivityCreated(Bundle _savedInstanceState) {
    super.onActivityCreated(_savedInstanceState);

    // As we are in a fragment, we need the parent activity instance in order to have access 
    // to the activity's SharedPreferences and the layout Views
    final Activity parentActivity = getActivity();

    SharedPreferences activityPreferences = parentActivity.getPreferences(Context.MODE_PRIVATE);

    mEditTextName = (EditText) parentActivity.findViewById(R.id.edittext_form_name);
    mEditTextDeveloper = (EditText) parentActivity.findViewById(R.id.edittext_form_developer);
    mDatePickerDate = (DatePicker) parentActivity.findViewById(R.id.datepicker_form_date);
    mEditTextURL = (EditText) parentActivity.findViewById(R.id.edittext_form_url);

    // if the last run of the activity wasn't finished using the Accept or Cancel buttons
    // then we must restore the state
    if (activityPreferences.getBoolean(FormAppActivity.STATE_NOT_SAVED, false)) {
        // Restore the name
        mEditTextName.setText(activityPreferences.getString(FormAppActivity.FORM_FIELD_NAME, ""));

        // Restore the developer name
        mEditTextDeveloper.setText(activityPreferences.getString(FormAppActivity.FORM_FIELD_DEVELOPER, ""));

        // Restore the calendar. The default values are extracted from Calendar, which is 
        // instantiated with the current date
        Calendar calendar = Calendar.getInstance();
        int day = activityPreferences.getInt(FormAppActivity.FORM_FIELD_DAY,
                calendar.get(Calendar.DAY_OF_MONTH));
        int month = activityPreferences.getInt(FormAppActivity.FORM_FIELD_MONTH,
                calendar.get(Calendar.MONTH) + 1);
        int year = activityPreferences.getInt(FormAppActivity.FORM_FIELD_YEAR, calendar.get(Calendar.YEAR));

        mDatePickerDate.updateDate(year, month, day);

        // Restore the URL
        mEditTextURL.setText(activityPreferences.getString(FormAppActivity.FORM_FIELD_URL, ""));

        // Finally, we restore the state of STATE_NOT_SAVED to false
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putBoolean(FormAppActivity.STATE_NOT_SAVED, false);
        editor.commit();//www. j a  v a  2 s  .c  o m
    }

    Button cleanButton = (Button) parentActivity.findViewById(R.id.button_form_clean);
    cleanButton.setOnClickListener(new OnClickListener() {
        // When the button is clicked, we erase the content of the controls
        // o reset them to the default value
        @Override
        public void onClick(View v) {
            mEditTextName.setText("");
            mEditTextDeveloper.setText("");
            Calendar calendar = Calendar.getInstance();
            mDatePickerDate.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
                    calendar.get(Calendar.DAY_OF_MONTH));
            mEditTextURL.setText("");
        }
    });

    Button acceptButton = (Button) parentActivity.findViewById(R.id.button_form_accept);
    acceptButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, mDatePickerDate.getDayOfMonth());
            calendar.set(Calendar.MONTH, mDatePickerDate.getMonth() - 1);
            calendar.set(Calendar.YEAR, mDatePickerDate.getYear());

            String name = mEditTextName.getText().toString();
            String developer = mEditTextDeveloper.getText().toString();
            String url = mEditTextURL.getText().toString();

            Intent intent = new Intent(parentActivity, ResultsActivity.class);

            // Store the data in the intent bundle
            intent.putExtra("name", name);
            intent.putExtra("developer", name);
            intent.putExtra("date", mDatePickerDate.getDayOfMonth() + "/" + mDatePickerDate.getMonth() + "/"
                    + mDatePickerDate.getYear());
            intent.putExtra("url", name);

            // Store the data for the App Singleton
            MyApplication.getInstance().setData(AppData.getInstance(name, developer, calendar, url));

            // Store the data in the database
            DatabaseAdapter databaseAdapter = new DatabaseAdapter(parentActivity);
            databaseAdapter.open();
            databaseAdapter.insertApp(name, developer, calendar, url);
            databaseAdapter.close();

            // Indicating that the activity is finished by the use of the button
            ((FormAppActivity) parentActivity).setButtonPressed();

            startActivity(intent);
            // As we have finished with this activity, we finalized it so 
            // the user never returns to it when the back button is pressed
            parentActivity.finish();
        }
    });

    Button cancelButton = (Button) parentActivity.findViewById(R.id.button_form_cancel);
    cancelButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Finishing the activity by the use of the button
            ((FormAppActivity) parentActivity).setButtonPressed();
            parentActivity.finish();
        }
    });
}

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

@NonNull
@Override//w  ww.j  a  va2  s .  c o m
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Activity activity = getActivity();
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    final LayoutInflater inflater = activity.getLayoutInflater();
    View view = inflater.inflate(R.layout.privacy_dialog, null);
    SharedPreferences preferences = activity.getPreferences(0);
    final SharedPreferences.Editor editor = preferences.edit();

    builder.setView(view).setTitle(R.string.privacy_title).setPositiveButton(R.string.help_ok,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    return builder.create();
}

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

@NonNull
@Override/* w w  w.j  a v  a2s. c  o m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Activity activity = getActivity();
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    final LayoutInflater inflater = activity.getLayoutInflater();
    View view = inflater.inflate(R.layout.help_dialog, null);
    SharedPreferences preferences = activity.getPreferences(0);
    final SharedPreferences.Editor editor = preferences.edit();

    builder.setView(view).setTitle(R.string.help_title).setPositiveButton(R.string.help_ok,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    Switch locationToggle = (Switch) view.findViewById(R.id.help_toggle);
    locationToggle.setChecked(preferences.getBoolean(StorageKeys.HELP_TOGGLE_KEY, false));
    locationToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            editor.putBoolean(StorageKeys.HELP_TOGGLE_KEY, b);
            editor.commit();
        }
    });

    return builder.create();
}

From source file:com.juick.android.TagsFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/* w  ww.j  a v  a2 s .c  o  m*/
        parentActivity = (TagsFragmentListener) activity;
        sp = activity.getPreferences(Activity.MODE_PRIVATE);
        if (saveMineAll)
            showMine = sp.getBoolean("showMine", true);
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement TagsFragmentListener");
    }
}

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

private void setZops(boolean force) {
    Activity activity = getActivity();
    adapter = new AllZopsGridViewAdapter(activity, R.id.gridItem, types);

    final SharedPreferences preferences = activity.getPreferences(0);
    final SharedPreferences.Editor editor = preferences.edit();

    if (!preferences.contains(StorageKeys.ZOPCATEGORY_KEY)) {
        InputStream is = getResources().openRawResource(R.raw.zops);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        HashMap<String, MobileZop> temp = new HashMap<>();

        types = new ArrayList<>(
                (ArrayList<MobileZop>) new Gson().fromJson(reader, new TypeToken<ArrayList<MobileZop>>() {
                }.getType()));/*from   w w  w . j a v  a  2s . c o m*/
        for (MobileZop type : types) {
            String key = type.Type.toString();

            if (!temp.containsKey(key)) {
                temp.put(key, type);
            }
        }
        types = new ArrayList<>(temp.values());
        if (adapter != null) {
            adapter.setObjects(types);
            adapter.notifyDataSetChanged();
        }

        try {
            FileOutputStream fos = activity.openFileOutput(StorageKeys.ZOPCATEGORY_KEY, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(types);
            oos.close();
            fos.close();
            editor.putString(StorageKeys.ZOPCATEGORY_KEY, Integer.toString(types.size()));
            editor.commit();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        if (preferences.contains(StorageKeys.ZOPCATEGORY_KEY)) {
            try {
                FileInputStream fis = activity.openFileInput(StorageKeys.ZOPCATEGORY_KEY);
                ObjectInputStream ois = new ObjectInputStream(fis);

                types = (ArrayList) ois.readObject();
                ois.close();
                fis.close();

                if (adapter != null) {
                    adapter.setObjects(types);
                    adapter.notifyDataSetChanged();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        ListenableFuture<JsonElement> result = MainActivity.MobileClient.invokeApi("mobileZop", "GET",
                new ArrayList<Pair<String, String>>() {
                    {
                        add(new Pair<String, String>("count", "true"));
                    }
                });

        Futures.addCallback(result, new FutureCallback<JsonElement>() {

            @Override
            public void onSuccess(JsonElement result) {
                Activity activity = getActivity();
                JsonArray typesAsJson = result.getAsJsonArray();
                if (typesAsJson != null) {
                    types = new Gson().fromJson(result, new TypeToken<ArrayList<MobileZop>>() {
                    }.getType());
                    try {
                        FileOutputStream fos = activity.openFileOutput(StorageKeys.ZOPCATEGORY_KEY,
                                Context.MODE_PRIVATE);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);

                        oos.writeObject(types);
                        oos.close();
                        fos.close();
                        editor.putString(StorageKeys.ZOPCATEGORY_KEY, Integer.toString(types.size()));
                        editor.commit();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (adapter != null) {
                    adapter.setObjects(types);
                    GridView zops = (GridView) activity.findViewById(R.id.gridViewZops);
                    RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                    loader.setVisibility(View.INVISIBLE);
                    zops.setVisibility(View.VISIBLE);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(@NonNull Throwable t) {
                Activity activity = getActivity();
                if (activity != null && types.size() == 0) {
                    GridView zops = (GridView) activity.findViewById(R.id.gridViewZops);
                    RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                    zops.setVisibility(View.INVISIBLE);
                    loader.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}