Home | History | Annotate | Download | only in com.example.android.autobackupsample
      1 /*
      2  * Copyright (C) 2015 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 package com.example.android.autobackupsample;
     18 
     19 import android.app.Fragment;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.os.Environment;
     23 import android.util.Log;
     24 import android.view.LayoutInflater;
     25 import android.view.Menu;
     26 import android.view.MenuInflater;
     27 import android.view.MenuItem;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.widget.ArrayAdapter;
     31 import android.widget.ListView;
     32 import android.widget.TextView;
     33 
     34 import java.io.File;
     35 import java.text.NumberFormat;
     36 import java.util.ArrayList;
     37 
     38 
     39 /**
     40  * A placeholder fragment containing a simple view.
     41  */
     42 public class MainActivityFragment extends Fragment {
     43 
     44     private static final String TAG = "AutoBackupSample";
     45     public static final int ADD_FILE_REQUEST = 1;
     46     public static final int ADD_FILE_RESULT_SUCCESS = 101;
     47     public static final int ADD_FILE_RESULT_ERROR = 102;
     48 
     49     private ArrayAdapter<File> mFilesArrayAdapter;
     50     private ArrayList<File> mFiles;
     51 
     52     public MainActivityFragment() {
     53     }
     54 
     55     @Override
     56     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     57                              Bundle savedInstanceState) {
     58         setHasOptionsMenu(true);
     59         return inflater.inflate(R.layout.fragment_main, container, false);
     60     }
     61 
     62     @Override
     63     public void onActivityResult(int requestCode, int resultCode, Intent data) {
     64         super.onActivityResult(requestCode, resultCode, data);
     65 
     66         if (requestCode == ADD_FILE_REQUEST && resultCode == ADD_FILE_RESULT_SUCCESS) {
     67             updateListOfFiles();
     68         }
     69     }
     70 
     71     @Override
     72     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
     73         // Inflate the menu; this adds items to the action bar if it is present.
     74         inflater.inflate(R.menu.menu_main, menu);
     75         super.onCreateOptionsMenu(menu, inflater);
     76     }
     77 
     78     @Override
     79     public boolean onOptionsItemSelected(MenuItem item) {
     80         // Handle action bar item clicks here. The action bar will
     81         // automatically handle clicks on the Home/Up button, so long
     82         // as you specify a parent activity in AndroidManifest.xml.
     83         int id = item.getItemId();
     84 
     85         //noinspection SimplifiableIfStatement
     86         if (id == R.id.action_settings) {
     87             return true;
     88         } else if (id == R.id.action_add_file) {
     89             Intent addFileIntent = new Intent(getActivity(), AddFileActivity.class);
     90             startActivityForResult(addFileIntent, ADD_FILE_REQUEST);
     91             return true;
     92         }
     93 
     94         return super.onOptionsItemSelected(item);
     95     }
     96 
     97     @Override
     98     public void onResume() {
     99         super.onResume();
    100         if (mFilesArrayAdapter == null) {
    101             mFiles = createListOfFiles();
    102             mFilesArrayAdapter = new ArrayAdapter<File>(getActivity(),
    103                     R.layout.file_list_item, mFiles) {
    104 
    105                 @Override
    106                 public View getView(int position, View convertView, ViewGroup parent) {
    107                     LayoutInflater inflater = LayoutInflater.from(getContext());
    108                     View itemView = inflater.inflate(R.layout.file_list_item, parent, false);
    109                     TextView fileNameView = (TextView) itemView.findViewById(R.id.file_name);
    110                     String fileName = getItem(position).getAbsolutePath();
    111                     fileNameView.setText(fileName);
    112                     TextView fileSize = (TextView) itemView.findViewById(R.id.file_size);
    113                     String fileSizeInBytes = NumberFormat.getInstance()
    114                             .format(getItem(position).length());
    115                     fileSize.setText(fileSizeInBytes);
    116                     return itemView;
    117                 }
    118             };
    119             updateListOfFiles();
    120             ListView filesListView = (ListView) getView().findViewById(R.id.file_list);
    121             filesListView.setAdapter(mFilesArrayAdapter);
    122         }
    123     }
    124 
    125     private ArrayList<File> createListOfFiles() {
    126         ArrayList<File> listOfFiles = new ArrayList<File>();
    127         addFilesToList(listOfFiles, getActivity().getFilesDir());
    128         if (Utils.isExternalStorageAvailable()) {
    129             addFilesToList(listOfFiles, getActivity().getExternalFilesDir(null));
    130         }
    131         addFilesToList(listOfFiles, getActivity().getNoBackupFilesDir());
    132         return listOfFiles;
    133     }
    134 
    135     private void addFilesToList(ArrayList<File> listOfFiles, File dir) {
    136         File[] files = dir.listFiles();
    137         for (File file: files) {
    138             listOfFiles.add(file);
    139         }
    140     }
    141 
    142     public void updateListOfFiles() {
    143         TextView emptyFileListMessage =
    144                 (TextView) getView().findViewById(R.id.empty_file_list_message);
    145         mFiles = createListOfFiles();
    146         if (mFilesArrayAdapter.getCount() > 0) {
    147             mFilesArrayAdapter.clear();
    148         }
    149         for (File file: mFiles) {
    150             mFilesArrayAdapter.add(file);
    151         }
    152         // Display a message instructing to add files if no files found.
    153         if (mFiles.size() == 0) {
    154             emptyFileListMessage.setVisibility(View.VISIBLE);
    155         } else {
    156             emptyFileListMessage.setVisibility(View.GONE);
    157         }
    158     }
    159 }
    160 
    161