Example usage for android.content SharedPreferences contains

List of usage examples for android.content SharedPreferences contains

Introduction

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

Prototype

boolean contains(String key);

Source Link

Document

Checks whether the preferences contains a preference.

Usage

From source file:org.rebo.app.TileMap.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Debug/*from   w w  w . j ava  2  s  . c o m*/
    RemoteDebugger.setExceptionHandler(this);

    // Set VTM preferences
    Parameters.ANIMATOR2 = true;
    Parameters.CUSTOM_COORD_SCALE = true;
    MapRenderer.COORD_SCALE = 2.0f;

    // Init view
    setContentView(R.layout.activity_tilemap_nav);
    App.view = (MapView) findViewById(R.id.mapView);
    registerMapView(App.view);

    App.map = mMap;
    activity = this;

    // Keep screen on
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    mMapLayers = new MapLayers();
    mMapLayers.setBaseMap(this);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    if (!prefs.contains("distanceTouch"))
        prefs.edit().putBoolean("distanceTouch", true).apply();

    if (prefs.getBoolean("distanceTouch", true)) {
        mDistanceTouch = new DistanceTouchOverlay(mMap, this);
        mMap.layers().add(mDistanceTouch);
    }

    mCompass = new Compass(this, mMap);
    mMap.layers().add(mCompass);

    mLocation = new LocationHandler(this, mCompass);
    mLocation.addVirtualLocationListener(mCompass);

    App.poiSearch = new POISearch(); // TODO remove

    // Init POIs, must be set after MapLayers
    App.poiManager = new PoiManager();
    App.poiManager.loadPreferences(this);

    App.routeSearch = new RouteSearch();
    routeSearch.addRouteSearchListener(this);

    registerForContextMenu(App.view);
    //Navigationview
    mToolbar = (LinearLayout) findViewById(R.id.toolbar);
    mSearchBar = (EditText) findViewById(R.id.search_bar);
    mSearchBar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(activity, PoiSearchActivity.class));
        }
    });
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationView = (NavigationView) findViewById(R.id.nav_view);
    mNavigationView.setNavigationItemSelectedListener(this);

    mCompassFrame = (FrameLayout) activity.findViewById(R.id.compass_wrapper);
    mCompassFab = (FloatingActionButton) activity.findViewById(R.id.compass);
    mCompassFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            toggleCompass(null);
        }
    });
    mCompassFab.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            toggleCompass(Compass.Mode.C3D);
            return true;
        }
    });
    mLocationFab = (FloatingActionButton) activity.findViewById(R.id.location);
    mLocationFab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            toggleLocation(null);
        }
    });
    mLocationFab.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Navigation nav;
            if (routeSearch != null && (nav = routeSearch.getNavigation()) != null) {
                BoundingBox bbox = nav.getRouteBounds();
                if (bbox != null) {
                    bbox = bbox.extendMargin(2f);
                    mMap.animator().animateTo(bbox);
                    mMap.updateMap(true);
                }
            }
            //toggleLocation(LocationHandler.Mode.SNAP);
            return true;
        }
    });

    handleIntent(getIntent(), true);
    // Here, this.Activity is the current activity

    Permission.requestLocationPermission();

    delayBlankMode();
}

From source file:org.mozilla.gecko.home.HomeConfigPrefsBackend.java

@Override
public String getLocale() {
    final SharedPreferences prefs = getSharedPreferences();

    String locale = prefs.getString(PREFS_LOCALE_KEY, null);
    if (locale == null) {
        // Initialize config with the current locale
        final String currentLocale = Locale.getDefault().toString();

        final SharedPreferences.Editor editor = prefs.edit();
        editor.putString(PREFS_LOCALE_KEY, currentLocale);
        editor.apply();/*w ww  . j  a  v  a 2s . c  o  m*/

        // If the user has saved HomeConfig before, return null this
        // one time to trigger a refresh and ensure we use the
        // correct locale for the saved state. For more context,
        // see HomePanelsManager.onLocaleReady().
        if (!prefs.contains(PREFS_CONFIG_KEY)) {
            locale = currentLocale;
        }
    }

    return locale;
}

From source file:com.android.providers.downloads.DownloadService.java

private String getXunleiPeerid() {
    SharedPreferences pf = getApplicationContext().getSharedPreferences(XLConfig.PREF_NAME,
            Context.MODE_WORLD_WRITEABLE);
    if (!pf.contains(XLConfig.PREF_KEY_XUNLEI_PEERID)) {
        String curPeerid = XLUtil.getPeerid(getApplicationContext());
        SharedPreferences.Editor et = pf.edit();
        et.putString(XLConfig.PREF_KEY_XUNLEI_PEERID, curPeerid);
        et.commit();//from w w w .j a v  a  2s .c  om
        return curPeerid;

    } else {
        String dbPeerid = pf.getString(XLConfig.PREF_KEY_XUNLEI_PEERID, XLConfig.DEFAULT_PEERID);
        if (dbPeerid.equals(XLConfig.DEFAULT_PEERID)) {
            dbPeerid = XLUtil.getPeerid(getApplicationContext());
            SharedPreferences.Editor et = pf.edit();
            et.putString(XLConfig.PREF_KEY_XUNLEI_PEERID, dbPeerid);
            et.commit();
        }
        return dbPeerid;
    }
}

From source file:org.awesomeapp.messenger.MainActivity.java

public void handleLock() {
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    if (settings.contains(ImApp.PREFERENCE_KEY_TEMP_PASS)) {
        //need to setup new user passphrase
        Intent intent = new Intent(this, LockScreenActivity.class);
        intent.setAction(LockScreenActivity.ACTION_CHANGE_PASSPHRASE);
        startActivity(intent);//www. ja  va  2  s  . c  o  m
    } else {

        //time to do the lock
        Intent intent = new Intent(this, RouterActivity.class);
        intent.setAction(RouterActivity.ACTION_LOCK_APP);
        startActivity(intent);
        finish();
    }
}

From source file:github.popeen.dsub.activity.SubsonicFragmentActivity.java

private void loadSettings() {
    PreferenceManager.setDefaultValues(this, R.xml.settings_appearance, false);
    PreferenceManager.setDefaultValues(this, R.xml.settings_cache, false);
    PreferenceManager.setDefaultValues(this, R.xml.settings_drawer, false);
    PreferenceManager.setDefaultValues(this, R.xml.settings_sync, false);
    PreferenceManager.setDefaultValues(this, R.xml.settings_playback, false);

    SharedPreferences prefs = Util.getPreferences(this);
    if (!prefs.contains(Constants.PREFERENCES_KEY_CACHE_LOCATION)
            || prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null) == null) {
        resetCacheLocation(prefs);/*w w w .  j a v a 2s. co  m*/
    } else {
        String path = prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null);
        File cacheLocation = new File(path);
        if (!FileUtil.verifyCanWrite(cacheLocation)) {
            // Only warn user if there is a difference saved
            if (resetCacheLocation(prefs)) {
                Util.info(this, R.string.common_warning, R.string.settings_cache_location_reset);
            }
        }
    }

    if (!prefs.contains(Constants.PREFERENCES_KEY_OFFLINE)) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(Constants.PREFERENCES_KEY_OFFLINE, false);

        editor.putString(Constants.PREFERENCES_KEY_SERVER_NAME + 1, "Demo Server");
        editor.putString(Constants.PREFERENCES_KEY_SERVER_URL + 1, "http://demo.subsonic.org");
        editor.putString(Constants.PREFERENCES_KEY_USERNAME + 1, "guest");
        editor.putString(Constants.PREFERENCES_KEY_PASSWORD + 1, "guest");
        editor.putInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
        editor.commit();
    }
    if (!prefs.contains(Constants.PREFERENCES_KEY_SERVER_COUNT)) {
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, 1);
        editor.commit();
    }
}

From source file:dynamite.zafroshops.app.MainActivity.java

public void getLocation(boolean force) {
    SharedPreferences preferences = getPreferences(0);
    SharedPreferences.Editor editor = preferences.edit();
    boolean locationToggle = preferences.getBoolean(StorageKeys.LOCATION_TOGGLE_KEY, locationToggleDefault);

    if (!force && preferences.contains(StorageKeys.LATITUDE_KEY)) {
        LastLocation = new LocationBase();
        LastLocation.Latitude = getDouble(preferences, StorageKeys.LATITUDE_KEY, 0);
        LastLocation.Longitude = getDouble(preferences, StorageKeys.LONGITUDE_KEY, 0);
        if (locationToggle) {
            getAddress(true);/*from   w  ww.j  a v a  2  s  .c om*/
        }
    } else if (locationToggle) {
        AndroidLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

        if (AndroidLastLocation != null) {
            LastLocation = new LocationBase();
            LastLocation.Latitude = AndroidLastLocation.getLatitude();
            LastLocation.Longitude = AndroidLastLocation.getLongitude();

            putDouble(editor, StorageKeys.LATITUDE_KEY, LastLocation.Latitude);
            putDouble(editor, StorageKeys.LONGITUDE_KEY, LastLocation.Longitude);
            editor.commit();
            getAddress(force);
        }
    }
}

From source file:org.awesomeapp.messenger.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSearchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));

    if (mSearchView != null) {
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        mSearchView.setIconifiedByDefault(false);

        SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
            public boolean onQueryTextChange(String query) {
                mConversationList.doSearch(query);
                return true;
            }//from w  w  w.  jav  a  2s . co m

            public boolean onQueryTextSubmit(String query) {
                mConversationList.doSearch(query);

                return true;
            }
        };

        mSearchView.setOnQueryTextListener(queryTextListener);

        mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                mConversationList.doSearch(null);
                return false;
            }
        });
    }

    MenuItem mItem = menu.findItem(R.id.menu_lock_reset);

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    if (!settings.contains(ImApp.PREFERENCE_KEY_TEMP_PASS))
        mItem.setVisible(true);

    return true;
}

From source file:org.openhab.habdroid.ui.OpenHABWidgetListActivity.java

/**
 * This method is called by AsyncServiceResolver in case of successful service discovery
 * to start connection with local openHAB instance
 *//*from   w w w . ja  v  a2  s . c  o  m*/
public void onServiceResolved(ServiceInfo serviceInfo) {
    Log.i(TAG, "Service resolved: " + serviceInfo.getHostAddresses()[0] + " port:" + serviceInfo.getPort());
    openHABBaseUrl = "https://" + serviceInfo.getHostAddresses()[0] + ":"
            + String.valueOf(serviceInfo.getPort()) + "/";
    //      progressDialog.hide();
    progressDialog.dismiss();
    AsyncHttpClient asyncHttpClient = new MyAsyncHttpClient(this);
    asyncHttpClient.get(openHABBaseUrl + "static/uuid", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String content) {
            Log.i(TAG, "Got openHAB UUID = " + content);
            SharedPreferences settings = PreferenceManager
                    .getDefaultSharedPreferences(OpenHABWidgetListActivity.this);
            if (settings.contains("openhab_uuid")) {
                String openHABUUID = settings.getString("openhab_uuid", "");
                if (openHABUUID.equals(content)) {
                    Log.i(TAG, "openHAB UUID does match the saved one");
                    showTime();
                } else {
                    Log.i(TAG, "openHAB UUID doesn't match the saved one");
                    // TODO: need to add some user prompt here
                    /*                  Toast.makeText(getApplicationContext(), 
                                            "openHAB UUID doesn't match the saved one!",
                                            Toast.LENGTH_LONG).show();*/
                    showTime();
                }
            } else {
                Log.i(TAG, "No recorded openHAB UUID, saving the new one");
                Editor preferencesEditor = settings.edit();
                preferencesEditor.putString("openhab_uuid", content);
                preferencesEditor.commit();
                showTime();
            }
        }

        @Override
        public void onFailure(Throwable e, String errorResponse) {
            Toast.makeText(getApplicationContext(), getString(R.string.error_no_uuid), Toast.LENGTH_LONG)
                    .show();
        }
    });
}

From source file:com.anysoftkeyboard.AskPrefsImpl.java

private void updateStatistics(SharedPreferences sp, Context context) {
    boolean firstAppInstall = false;
    boolean firstVersionInstall = false;

    final String FIRST_APP_VERSION_INSTALL = context
            .getString(R.string.settings_key_first_app_version_installed);
    if (!sp.contains(FIRST_APP_VERSION_INSTALL)) {
        firstAppInstall = true;//w  w  w .  ja  v a  2s .c o m
    }

    final String LAST_APP_VERSION_INSTALLED = context
            .getString(R.string.settings_key_last_app_version_installed);
    if (sp.getInt(LAST_APP_VERSION_INSTALLED, 0) != BuildConfig.VERSION_CODE) {
        firstVersionInstall = true;
    }

    if (firstAppInstall || firstVersionInstall) {
        Editor editor = sp.edit();

        final long installTime = System.currentTimeMillis();
        if (firstAppInstall) {
            editor.putInt(FIRST_APP_VERSION_INSTALL, BuildConfig.VERSION_CODE);
            editor.putLong(context.getString(R.string.settings_key_first_time_app_installed), installTime);
        }

        if (firstVersionInstall) {
            editor.putInt(LAST_APP_VERSION_INSTALLED, BuildConfig.VERSION_CODE);
            editor.putLong(context.getString(R.string.settings_key_first_time_current_version_installed),
                    installTime);
        }
        SharedPreferencesCompat.EditorCompat.getInstance().apply(editor);
    }
}

From source file:com.autburst.picture.MainActivity.java

/** Called when the activity is first created. */
@Override/* ww  w .j av a  2s .co  m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Log.d(TAG, "------ onCreate MainActivity ------");

    setContentView(R.layout.main);

    albumsList = (ListView) findViewById(R.id.albumList);

    inflater = getLayoutInflater();

    List<String> albumsAsList = Utilities.getAlbumsAsList();
    adapter = new AlbumAdapter(inflater, this, R.layout.row, albumsAsList);
    adapter.sort(Utilities.getAlbumNameComparator());

    albumsList.setAdapter(adapter);
    albumsList.setOnItemClickListener(this);

    cancelDeletion = (Button) findViewById(R.id.cancelDeletion);
    cancelDeletion.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            hideDeletionPanelAndCB(MainActivityController.DELETE_BUTTON_PRESSED);
        }
    });

    deleteCheckedAlbums = (Button) findViewById(R.id.deleteCheckedAlbums);
    deleteCheckedAlbums.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            List<String> albums = Utilities.getAlbumsAsList();
            Collections.sort(albums, Utilities.getAlbumNameComparator());

            for (int i = 0; i < checkedAlbums.size(); i++) {
                // get checked rows
                int rowIndex = checkedAlbums.get(i);
                String albumName = albums.get(rowIndex);

                Log.d(TAG, "delete album: " + albumName + " at rowIndex: " + rowIndex + " decodedName: "
                        + new String(Base64.decodeBase64(albumName.getBytes())));

                // delete albums
                // remove files on sdcard
                File album = Utilities.getAlbumDirectory(albumName);
                File[] file = album.listFiles();
                for (File file2 : file) {
                    file2.delete();
                    Log.d(TAG, "deleted file: " + file2.getName());
                }
                album.delete();

                // remove shared preferences entry
                SharedPreferences preferences = getSharedPreferences(Utilities.PIC_STORE, 0);
                Editor edit = preferences.edit();
                String pref0 = albumName + ".id";
                String pref1 = albumName + ".portrait";
                String pref2 = albumName + ".videoId";
                String pref3 = albumName + ".frameRate";

                if (preferences.contains(pref0)) {
                    edit.remove(pref0);
                    Log.d(TAG, "deleted pref .id");
                }

                if (preferences.contains(pref1)) {
                    edit.remove(pref1);
                    Log.d(TAG, "deleted pref .portrait");
                }

                if (preferences.contains(pref2)) {
                    edit.remove(pref2);
                    Log.d(TAG, "deleted pref .videoId");
                }

                if (preferences.contains(pref3)) {
                    edit.remove(pref3);
                    Log.d(TAG, "deleted pref .frameRate");
                }

                edit.commit();

                adapter.remove(albumName);
                adapter.sort(Utilities.getAlbumNameComparator());
            }

            // hide deletion panel and checkboxes
            hideDeletionPanelAndCB(MainActivityController.DELETED_ALBUMS);

            // reload list
            adapter.notifyDataSetChanged();

            controller.transformGUI(MainActivityController.DELETED_ALBUMS);
        }
    });

    deletionPanel = (LinearLayout) findViewById(R.id.deletionPanel);
    deleteButton = (ImageButton) findViewById(R.id.deleteButton);

    deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            controller.transformGUI(MainActivityController.DELETE_BUTTON_PRESSED);

            if (deleteCheckboxesVisible) {
                // hide checkboxes
                deleteCheckboxesVisible = false;

                // make checkboxes invisible
                for (CheckBox cbToDelete : deleteAlbumCheckBoxes) {
                    if (cbToDelete.getVisibility() == View.VISIBLE) {
                        Animation myFadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this,
                                R.anim.slight_left_cb);
                        cbToDelete.startAnimation(myFadeInAnimation);
                        cbToDelete.setVisibility(View.GONE);
                        if (cbToDelete.isChecked())
                            cbToDelete.setChecked(false);

                        Log.d(TAG, "set delete checkbox GONE");
                    }
                }

            } else {
                // show checkboxes
                deleteCheckboxesVisible = true;

                for (CheckBox cbToDelete : deleteAlbumCheckBoxes) {
                    if (cbToDelete.getVisibility() == View.GONE) {
                        // checkboxes

                        Animation myFadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this,
                                R.anim.slight_right_cb);
                        cbToDelete.startAnimation(myFadeInAnimation);
                        cbToDelete.setVisibility(View.VISIBLE);

                        Log.d(TAG, "set delete checkbox visible");
                    }
                }
            }
        }
    });

    openButton = (ImageButton) findViewById(R.id.openButton);

    openButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (openButtonsVisible) {
                // hide open Buttons
                AnimationHelper.fadeInFromLeftToRight(MainActivity.this, deleteButton);

                // make buttons gone
                for (Button openAlbum : openAlbumButtons) {
                    if (openAlbum.getVisibility() == View.VISIBLE) {
                        Animation myFadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this,
                                R.anim.slight_right);
                        openAlbum.startAnimation(myFadeInAnimation);
                        openAlbum.setVisibility(View.GONE);
                    }
                }
                openButtonsVisible = false;
            } else {
                // show open buttons
                AnimationHelper.fadeOutToLeftSide(MainActivity.this, deleteButton);

                // make buttons visible
                for (Button openAlbum : openAlbumButtons) {
                    if (openAlbum.getVisibility() == View.GONE) {
                        Animation myFadeInAnimation = AnimationUtils.loadAnimation(MainActivity.this,
                                R.anim.slight_left);
                        openAlbum.startAnimation(myFadeInAnimation);
                        openAlbum.setVisibility(View.VISIBLE);
                    }
                }

                openButtonsVisible = true;
            }

        }
    });

    controller = new MainActivityController(this, openButton, deleteButton, deletionPanel);
}