Java tutorial
package com.joeyturczak.jtscanner.ui; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.joeyturczak.jtscanner.R; import com.joeyturczak.jtscanner.adapters.FileListAdapter; import com.joeyturczak.jtscanner.utils.Utility; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * Created by joeyturczak on 9/16/15. * Copyright (C) 2015 Joey Turczak * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ public class FileListFragment extends Fragment { public static final long ONE_DAY = 86400000; private ListView mFileList; private FileListAdapter mFileListAdapter; private List<String> mFileNames; private Toast mToast; public FileListFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_file_list, container, false); setHasOptionsMenu(true); mFileNames = new ArrayList<>(); mFileList = (ListView) rootView.findViewById(R.id.file_list); mFileListAdapter = new FileListAdapter(getActivity(), R.layout.file_list_item, mFileNames); mFileList.setAdapter(mFileListAdapter); loadFileList(); mFileList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { openFile(position); } }); return rootView; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); MenuItem menuItem = menu.findItem(R.id.action_save_spreadsheet); menuItem.setVisible(true); Utility.tintMenuIcon(getContext(), menuItem, R.color.white); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_save_spreadsheet) { loadFileList(); mFileListAdapter.notifyDataSetChanged(); return true; } return super.onOptionsItemSelected(item); } private void loadFileList() { mFileListAdapter.clear(); File externalDir = Environment.getExternalStorageDirectory(); String externalDirPath = externalDir.getPath(); File scannerDir = new File(externalDirPath + getString(R.string.file_directory)); long today = Utility.normalizeDate(System.currentTimeMillis()); int daysToKeep = Utility.getDaysToKeepFiles(getActivity()); long daysToKeepMilliseconds = ONE_DAY * daysToKeep; long todayMinusDaysToKeep = today - daysToKeepMilliseconds; File[] files = scannerDir.listFiles(); if (files != null) { Arrays.sort(files, Collections.reverseOrder()); for (File file : files) { String fileName = file.getName(); fileName = fileName.replace("JTS_", ""); fileName = fileName.replace(".xls", ""); long fileDate = Utility.dateToMilliseconds(fileName, "MM-dd-yyyy"); if (fileDate < todayMinusDaysToKeep) { file.delete(); Utility.trimCache(getActivity()); } else { mFileListAdapter.add(file.getName()); } } } } private void openFile(final int position) { File externalDir = Environment.getExternalStorageDirectory(); String externalDirPath = externalDir.getPath(); final File scannerDir = new File(externalDirPath + getString(R.string.file_directory)); File file = new File(scannerDir + File.separator + mFileNames.get(position)); final Uri path = Uri.fromFile(file); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(path, getString(R.string.excel_data_type)); try { startActivity(intent); } catch (Exception e) { if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(getActivity(), R.string.toast_no_app_found, Toast.LENGTH_SHORT); mToast.show(); } } }