Example usage for android.preference PreferenceManager setDefaultValues

List of usage examples for android.preference PreferenceManager setDefaultValues

Introduction

In this page you can find the example usage for android.preference PreferenceManager setDefaultValues.

Prototype

public static void setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode,
        int resId, boolean readAgain) 

Source Link

Document

Similar to #setDefaultValues(Context,int,boolean) but allows the client to provide the filename and mode of the shared preferences file.

Usage

From source file:com.dmsl.anyplace.UnifiedNavigationActivity.java

/** Called when the activity is first created. */
@Override// w w  w.  j a  v  a 2  s.c  om
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_unifiednav);

    detectedAPs = (TextView) findViewById(R.id.detectedAPs);
    textFloor = (TextView) findViewById(R.id.textFloor);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    textDebug = (TextView) findViewById(R.id.textDebug);
    if (AnyplaceAPI.DEBUG_MESSAGES)
        textDebug.setVisibility(View.VISIBLE);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);

    userData = new AnyUserData();

    SimpleWifiManager.getInstance().startScan();
    sensorsMain = new SensorsMain(getApplicationContext());
    movementDetector = new MovementDetector();
    sensorsMain.addListener(movementDetector);
    sensorsStepCounter = new SensorsStepCounter(getApplicationContext(), sensorsMain);
    lpTracker = new TrackerLogicPlusIMU(movementDetector, sensorsMain, sensorsStepCounter);
    // lpTracker = new TrackerLogic(sensorsMain);
    floorSelector = new Algo1Radiomap(getApplicationContext());

    mAnyplaceCache = AnyplaceCache.getInstance(this);
    visiblePois = new VisiblePois();

    setUpMapIfNeeded();

    // setup the trackme button overlaid in the map
    btnTrackme = (ImageButton) findViewById(R.id.btnTrackme);
    btnTrackme.setImageResource(R.drawable.dark_device_access_location_off);
    isTrackingErrorBackground = true;
    btnTrackme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final GeoPoint gpsLoc = userData.getLocationGPSorIP();
            if (gpsLoc != null) {
                AnyplaceCache mAnyplaceCache = AnyplaceCache.getInstance(UnifiedNavigationActivity.this);
                mAnyplaceCache.loadWorldBuildings(new FetchBuildingsTaskListener() {

                    @Override
                    public void onSuccess(String result, List<BuildingModel> buildings) {
                        final FetchNearBuildingsTask nearest = new FetchNearBuildingsTask();
                        nearest.run(buildings, gpsLoc.lat, gpsLoc.lng, 200);

                        if (nearest.buildings.size() > 0 && (userData.getSelectedBuildingId() == null
                                || !userData.getSelectedBuildingId().equals(nearest.buildings.get(0).buid))) {
                            floorSelector.Stop();
                            final FloorSelector floorSelectorAlgo1 = new Algo1Server(getApplicationContext());
                            final ProgressDialog floorSelectorDialog = new ProgressDialog(
                                    UnifiedNavigationActivity.this);

                            floorSelectorDialog.setIndeterminate(true);
                            floorSelectorDialog.setTitle("Detecting floor");
                            floorSelectorDialog.setMessage("Please be patient...");
                            floorSelectorDialog.setCancelable(true);
                            floorSelectorDialog.setCanceledOnTouchOutside(false);
                            floorSelectorDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                                @Override
                                public void onCancel(DialogInterface dialog) {
                                    floorSelectorAlgo1.Destoy();
                                    bypassSelectBuildingActivity(nearest.buildings.get(0), "0", false);
                                }
                            });

                            class Callback implements ErrorAnyplaceFloorListener, FloorAnyplaceFloorListener {

                                @Override
                                public void onNewFloor(String floor) {
                                    floorSelectorAlgo1.Destoy();
                                    if (floorSelectorDialog.isShowing()) {
                                        floorSelectorDialog.dismiss();
                                        bypassSelectBuildingActivity(nearest.buildings.get(0), floor, false);
                                    }
                                }

                                @Override
                                public void onFloorError(Exception ex) {
                                    floorSelectorAlgo1.Destoy();
                                    if (floorSelectorDialog.isShowing()) {
                                        floorSelectorDialog.dismiss();
                                        bypassSelectBuildingActivity(nearest.buildings.get(0), "0", false);
                                    }
                                }

                            }
                            Callback callback = new Callback();
                            floorSelectorAlgo1.addListener((FloorAnyplaceFloorListener) callback);
                            floorSelectorAlgo1.addListener((ErrorAnyplaceFloorListener) callback);

                            // Show Dialog
                            floorSelectorDialog.show();
                            floorSelectorAlgo1.Start(gpsLoc.lat, gpsLoc.lng);
                        } else {
                            focusUserLocation();

                            // Clear cancel request
                            lastFloor = null;
                            floorSelector.RunNow();
                            lpTracker.reset();
                        }
                    }

                    @Override
                    public void onErrorOrCancel(String result) {

                    }

                }, UnifiedNavigationActivity.this, false);
            } else {
                focusUserLocation();

                // Clear cancel request
                lastFloor = null;
                floorSelector.RunNow();
                lpTracker.reset();
            }

        }
    });

    btnFloorUp = (ImageButton) findViewById(R.id.btnFloorUp);
    btnFloorUp.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (!userData.isFloorSelected()) {
                Toast.makeText(getBaseContext(), "Load a map before tracking can be used!", Toast.LENGTH_SHORT)
                        .show();
                return;
            }

            BuildingModel b = userData.getSelectedBuilding();
            if (b == null) {
                return;
            }

            if (userData.isNavBuildingSelected()) {
                // Move to start/destination poi's floor
                String floor_number;
                List<PoisNav> puids = userData.getNavPois();
                // Check start and destination floor number
                if (!puids.get(puids.size() - 1).floor_number.equals(puids.get(0).floor_number)) {
                    if (userData.getSelectedFloorNumber().equals(puids.get(puids.size() - 1).floor_number)) {
                        floor_number = puids.get(0).floor_number;
                    } else {
                        floor_number = puids.get(puids.size() - 1).floor_number;
                    }

                    FloorModel floor = b.getFloorFromNumber(floor_number);
                    if (floor != null) {
                        bypassSelectBuildingActivity(b, floor);
                        return;
                    }
                }
            }

            // Move one floor up
            int index = b.getSelectedFloorIndex();

            if (b.checkIndex(index + 1)) {
                bypassSelectBuildingActivity(b, b.getFloors().get(index + 1));
            }

        }
    });

    btnFloorDown = (ImageButton) findViewById(R.id.btnFloorDown);
    btnFloorDown.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!userData.isFloorSelected()) {
                Toast.makeText(getBaseContext(), "Load a map before tracking can be used!", Toast.LENGTH_SHORT)
                        .show();
                return;
            }

            BuildingModel b = userData.getSelectedBuilding();
            if (b == null) {
                return;
            }

            if (userData.isNavBuildingSelected()) {
                // Move to start/destination poi's floor
                String floor_number;
                List<PoisNav> puids = userData.getNavPois();
                // Check start and destination floor number
                if (!puids.get(puids.size() - 1).floor_number.equals(puids.get(0).floor_number)) {
                    if (userData.getSelectedFloorNumber().equals(puids.get(puids.size() - 1).floor_number)) {
                        floor_number = puids.get(0).floor_number;
                    } else {
                        floor_number = puids.get(puids.size() - 1).floor_number;
                    }

                    FloorModel floor = b.getFloorFromNumber(floor_number);
                    if (floor != null) {
                        bypassSelectBuildingActivity(b, floor);
                        return;
                    }
                }
            }

            // Move one floor down
            int index = b.getSelectedFloorIndex();

            if (b.checkIndex(index - 1)) {
                bypassSelectBuildingActivity(b, b.getFloors().get(index - 1));
            }
        }

    });

    /*
     * Create a new location client, using the enclosing class to handle callbacks.
     */
    // Create the LocationRequest object
    mLocationRequest = LocationRequest.create();
    // Use high accuracy
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    // Set the update interval to 2 seconds
    mLocationRequest.setInterval(2000);
    // Set the fastest update interval to 1 second
    mLocationRequest.setFastestInterval(1000);
    mLocationClient = new LocationClient(this, this, this);
    // declare that this is the first time this Activity launched so make
    // the automatic building selection
    mAutomaticGPSBuildingSelection = true;

    // get/set settings
    PreferenceManager.setDefaultValues(this, SHARED_PREFS_ANYPLACE, MODE_PRIVATE, R.xml.preferences_anyplace,
            true);
    SharedPreferences preferences = getSharedPreferences(SHARED_PREFS_ANYPLACE, MODE_PRIVATE);
    preferences.registerOnSharedPreferenceChangeListener(this);
    lpTracker.setAlgorithm(preferences.getString("TrackingAlgorithm", "WKNN"));

    // handle the search intent
    handleIntent(getIntent());
}

From source file:com.aware.Aware_Preferences.java

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

    mSensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);

    //Start the Aware
    Intent startAware = new Intent(getApplicationContext(), Aware.class);
    startService(startAware);//  w  w  w  .j  a  va2s . c o m

    addPreferencesFromResource(R.xml.aware_preferences);
    setContentView(R.layout.aware_ui);

    navigationDrawer = (DrawerLayout) findViewById(R.id.aware_ui_main);
    navigationList = (ListView) findViewById(R.id.aware_navigation);
    navigationToggle = new ActionBarDrawerToggle(this, navigationDrawer, R.drawable.ic_drawer,
            R.string.drawer_open, R.string.drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (Build.VERSION.SDK_INT > 11) {
                getActionBar().setTitle(getTitle());
                invalidateOptionsMenu();
            }
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (Build.VERSION.SDK_INT > 11) {
                getActionBar().setTitle(getTitle());
                invalidateOptionsMenu();
            }
        }
    };

    navigationDrawer.setDrawerListener(navigationToggle);

    String[] options = { "Stream", "Sensors", "Plugins", "Studies" };
    NavigationAdapter nav_adapter = new NavigationAdapter(getApplicationContext(), options);
    navigationList.setAdapter(nav_adapter);
    navigationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            LinearLayout item_container = (LinearLayout) view.findViewById(R.id.nav_container);
            item_container.setBackgroundColor(Color.DKGRAY);

            for (int i = 0; i < navigationList.getChildCount(); i++) {
                if (i != position) {
                    LinearLayout other = (LinearLayout) navigationList.getChildAt(i);
                    LinearLayout other_item = (LinearLayout) other.findViewById(R.id.nav_container);
                    other_item.setBackgroundColor(Color.TRANSPARENT);
                }
            }

            Bundle animations = ActivityOptions.makeCustomAnimation(Aware_Preferences.this,
                    R.anim.anim_slide_in_left, R.anim.anim_slide_out_left).toBundle();
            switch (position) {
            case 0: //Stream
                Intent stream_ui = new Intent(Aware_Preferences.this, Stream_UI.class);
                startActivity(stream_ui, animations);
                break;
            case 1: //Sensors
                Intent sensors_ui = new Intent(Aware_Preferences.this, Aware_Preferences.class);
                startActivity(sensors_ui, animations);
                break;
            case 2: //Plugins
                Intent plugin_manager = new Intent(Aware_Preferences.this, Plugins_Manager.class);
                startActivity(plugin_manager, animations);
                break;
            case 3: //Studies
                if (Aware.getSetting(getApplicationContext(), "study_id").length() > 0) {
                    new Async_StudyData().execute(
                            Aware.getSetting(getApplicationContext(), Aware_Preferences.WEBSERVICE_SERVER));
                } else {
                    Intent join_study = new Intent(Aware_Preferences.this, CameraStudy.class);
                    startActivityForResult(join_study, REQUEST_JOIN_STUDY, animations);
                }
                break;
            }

            navigationDrawer.closeDrawer(navigationList);
        }
    });

    if (getActionBar() != null) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    SharedPreferences prefs = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
    if (prefs.getAll().isEmpty()
            && Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID).length() == 0) {
        is_first_time = true;
        PreferenceManager.setDefaultValues(getApplicationContext(), getPackageName(), Context.MODE_PRIVATE,
                R.xml.aware_preferences, true);
        prefs.edit().commit(); //commit changes
    } else {
        PreferenceManager.setDefaultValues(getApplicationContext(), getPackageName(), Context.MODE_PRIVATE,
                R.xml.aware_preferences, false);
    }

    Map<String, ?> defaults = prefs.getAll();
    for (Map.Entry<String, ?> entry : defaults.entrySet()) {
        if (Aware.getSetting(getApplicationContext(), entry.getKey()).length() == 0) {
            Aware.setSetting(getApplicationContext(), entry.getKey(), entry.getValue());
        }
    }

    if (Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID).length() == 0) {
        UUID uuid = UUID.randomUUID();
        Aware.setSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID, uuid.toString());
    }
}

From source file:com.aware.Aware.java

public static void reset(Context c) {
    if (!c.getPackageName().equals("com.aware"))
        return;/*w  w w. jav  a  2s  .c om*/

    String device_id = Aware.getSetting(c, Aware_Preferences.DEVICE_ID);

    //Remove all settings
    c.getContentResolver().delete(Aware_Settings.CONTENT_URI, null, null);

    //Read default client settings
    SharedPreferences prefs = c.getSharedPreferences(c.getPackageName(), Context.MODE_PRIVATE);
    PreferenceManager.setDefaultValues(c, c.getPackageName(), Context.MODE_PRIVATE, R.xml.aware_preferences,
            true);
    prefs.edit().commit();

    Map<String, ?> defaults = prefs.getAll();
    for (Map.Entry<String, ?> entry : defaults.entrySet()) {
        Aware.setSetting(c, entry.getKey(), entry.getValue());
    }

    //Restore old Device ID
    Aware.setSetting(c, Aware_Preferences.DEVICE_ID, device_id);

    //Turn off all active plugins
    Cursor active_plugins = c.getContentResolver().query(Aware_Plugins.CONTENT_URI, null,
            Aware_Plugins.PLUGIN_STATUS + "=" + Plugins_Manager.PLUGIN_ACTIVE, null, null);
    if (active_plugins != null && active_plugins.moveToFirst()) {
        do {
            Aware.stopPlugin(c,
                    active_plugins.getString(active_plugins.getColumnIndex(Aware_Plugins.PLUGIN_PACKAGE_NAME)));
        } while (active_plugins.moveToNext());
        active_plugins.close();
    }

    //Apply fresh state
    Intent aware_apply = new Intent(Aware.ACTION_AWARE_REFRESH);
    c.sendBroadcast(aware_apply);

    Intent preferences = new Intent(c, Aware_Preferences.class);
    preferences.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    c.startActivity(preferences);
}