Home | History | Annotate | Download | only in storageprovider
      1 /*
      2 * Copyright 2013 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 *     http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 
     17 
     18 package com.example.android.storageprovider;
     19 
     20 
     21 import android.content.SharedPreferences;
     22 import android.os.Bundle;
     23 import android.provider.DocumentsContract;
     24 import android.support.v4.app.Fragment;
     25 import android.view.Menu;
     26 import android.view.MenuItem;
     27 
     28 import com.example.android.common.logger.Log;
     29 
     30 /**
     31  * Toggles the user's login status via a login menu option, and enables/disables the cloud storage
     32  * content provider.
     33  */
     34 public class StorageProviderFragment extends Fragment {
     35 
     36     private static final String TAG = "StorageProviderFragment";
     37     private static final String AUTHORITY = "com.example.android.storageprovider.documents";
     38     private boolean mLoggedIn = false;
     39 
     40     @Override
     41     public void onCreate(Bundle savedInstanceState) {
     42         super.onCreate(savedInstanceState);
     43         mLoggedIn = readLoginValue();
     44 
     45         setHasOptionsMenu(true);
     46     }
     47 
     48     @Override
     49     public void onPrepareOptionsMenu(Menu menu) {
     50         super.onPrepareOptionsMenu(menu);
     51         MenuItem item = menu.findItem(R.id.sample_action);
     52         item.setTitle(mLoggedIn ? R.string.log_out : R.string.log_in);
     53     }
     54 
     55     @Override
     56     public boolean onOptionsItemSelected(MenuItem item) {
     57         if (item.getItemId() == R.id.sample_action) {
     58             toggleLogin();
     59             item.setTitle(mLoggedIn ? R.string.log_out : R.string.log_in);
     60 
     61             // BEGIN_INCLUDE(notify_change)
     62             // Notify the system that the status of our roots has changed.  This will trigger
     63             // a call to MyCloudProvider.queryRoots() and force a refresh of the system
     64             // picker UI.  It's important to call this or stale results may persist.
     65             getActivity().getContentResolver().notifyChange(DocumentsContract.buildRootsUri
     66                     (AUTHORITY), null, false);
     67             // END_INCLUDE(notify_change)
     68         }
     69         return true;
     70     }
     71 
     72     /**
     73      * Dummy function to change the user's authorization status.
     74      */
     75     private void toggleLogin() {
     76         // Replace this with your standard method of authentication to determine if your app
     77         // should make the user's documents available.
     78         mLoggedIn = !mLoggedIn;
     79         writeLoginValue(mLoggedIn);
     80         Log.i(TAG, getString(mLoggedIn ? R.string.logged_in_info : R.string.logged_out_info));
     81     }
     82 
     83     /**
     84      * Dummy function to save whether the user is logged in.
     85      */
     86     private void writeLoginValue(boolean loggedIn) {
     87         final SharedPreferences sharedPreferences =
     88                 getActivity().getSharedPreferences(getString(R.string.app_name),
     89                         getActivity().MODE_PRIVATE);
     90         sharedPreferences.edit().putBoolean(getString(R.string.key_logged_in), loggedIn).commit();
     91     }
     92 
     93     /**
     94      * Dummy function to determine whether the user is logged in.
     95      */
     96     private boolean readLoginValue() {
     97         final SharedPreferences sharedPreferences =
     98                 getActivity().getSharedPreferences(getString(R.string.app_name),
     99                         getActivity().MODE_PRIVATE);
    100         return sharedPreferences.getBoolean(getString(R.string.key_logged_in), false);
    101     }
    102 
    103 }
    104 
    105 
    106