Back to project page android_device.
The source code is released under:
[Apache License](http://www.apache.org/licenses/): Version 2.0, January 2004 =============== ## TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION ## ### 1. Definitions. ### "License" sha...
If you think the Android project android_device listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * ================================================================================================= * Copyright (C) 2013 - 2014 Martin Albedinsky [Wolf-ITechnologies] * ================================================================================================= * Licensed under the Apache License, Version 2.0 or later (further "License" only). * ------------------------------------------------------------------------------------------------- * You may use this file only in compliance with the License. More details and copy of this License * you may obtain at/*w w w.j ava 2s.c om*/ * * http://www.apache.org/licenses/LICENSE-2.0 * * You can redistribute, modify or publish any part of the code written within this file but as it * is described in the License, the software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF ANY KIND. * * See the License for the specific language governing permissions and limitations under the License. * ================================================================================================= */ package com.wit.android.device.examples.fragment; import android.app.ActionBar; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.ActionMode; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SearchView; import android.widget.TextView; import android.widget.Toast; import com.wit.android.device.AndroidDevice; import com.wit.android.device.Storage; import com.wit.android.device.examples.HomeActivity; import com.wit.android.device.examples.R; import com.wit.android.device.examples.adapter.FilesAdapter; import com.wit.android.device.examples.dialog.NewFileDialog; import com.wit.android.device.examples.module.StorageAssistant; import com.wit.android.device.util.StorageUtils; import com.wit.android.examples.dialog.ExSimpleDialog; import com.wit.android.examples.libs.fragment.ExListFragment; import com.wit.android.examples.libs.fragment.annotation.ExActionBarOptions; import com.wit.android.examples.libs.fragment.annotation.ExActionModeOptions; import com.wit.android.examples.libs.fragment.annotation.ExAdapterViewOptions; import com.wit.android.examples.libs.fragment.annotation.ExInjectView; import com.wit.android.examples.libs.fragment.annotation.ExInjectViews; import com.wit.android.examples.widget.ExNavigationButtons; import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.util.LinkedList; import java.util.List; /** * <p> * Description. * </p> * * @author Martin Albedinsky */ @ExInjectViews @ExActionModeOptions(menu = R.menu.action_menu_files) @ExActionBarOptions(title = R.string.Navigation_Label_StorageInterface) @ExAdapterViewOptions(emptyText = R.string.Storage_Files_Empty, longClickable = true) public class StorageFilesFragment extends ExListFragment<FilesAdapter> implements AdapterView.OnItemClickListener, NewFileDialog.OnDialogListener, StorageAssistant.StorageWatcher, StorageAssistant.LoaderCallback, ExNavigationButtons.OnButtonsListener { /** * Log TAG. */ private static final String TAG = StorageFilesFragment.class.getSimpleName(); /** * */ static final String BUNDLE_FILES_PATH = "com.wit.android.device.examples.fragment.StorageFilesFragment.BUNDLE.FilesPath"; /** * */ private final Storage STORAGE = AndroidDevice.getInstance(null).getStorage(); /** * */ private final String EXTERNAL_PACKAGE_DIR_PATH = STORAGE.getStoragePath(Storage.EXTERNAL_PACKAGE); /** * */ private final FileFilter FILTER = new FileFilter() { /** */ @Override public boolean accept(File pathname) { return (bFilterFiles && pathname.isFile()) || (bFilterDirectories && pathname.isDirectory()); } }; /** * */ private final FilenameFilter NAME_FILTER = new FilenameFilter() { /** */ @Override public boolean accept(File dir, String filename) { return TextUtils.isEmpty(mSearchQuery) || filename.contains(mSearchQuery); } }; /** * */ private final LinkedList<String> mFilesPathBackStack = new LinkedList<>(); @ExInjectView(R.id.fragment_storage_files_layout_loading) private View mLoadingView; @ExInjectView(value = R.id.fragment_storage_files_text_view_current_path, clickable = true) private TextView mTextCurrentPath; /** * */ private FilesAdapter mFilesAdapter; /** * */ private String mCurrentFilesPath; /** * */ private StorageAssistant mStorageAssistant; /** * */ private ExNavigationButtons mNavigationButtons; /** * */ private String mSearchQuery; /** * */ private boolean bInExternalPackageSection; /** * */ private boolean bFilterFiles, bFilterDirectories; { bFilterFiles = bFilterDirectories = true; } /** * * @param filesPath * @return */ public static Bundle createParams(String filesPath) { final Bundle params = new Bundle(); params.putString(BUNDLE_FILES_PATH, filesPath); return params; } /** */ @Override public void onNavigationButtonClicked(ExNavigationButtons buttons, int button) { switch (button) { case ExNavigationButtons.BUTTON_LEFT: mStorageAssistant.clearClipboard(); break; case ExNavigationButtons.BUTTON_RIGHT: this.setLoadingVisible(true); mStorageAssistant.performClipboardAction(mCurrentFilesPath); break; } this.setNavigationButtonsVisible(false); invalidateOptionsMenu(); } /** */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (isInActionMode()) { mFilesAdapter.dispatchItemSelected(position); return; } final File file = mFilesAdapter.getItem(position); if (file != null) { if (file.isDirectory()) { this.loadFilesForPath(file.getPath(), true); } else { Toast.makeText(getActivity(), "File preview not supported", Toast.LENGTH_SHORT).show(); } } } /** */ @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { return bInExternalPackageSection && super.onItemLongClick(parent, view, position, id); } /** */ @Override protected void onActionModeStarted(ActionMode actionMode, ListView adapterView, View view, int position, long id) { super.onActionModeStarted(actionMode, adapterView, view, position, id); mFilesAdapter.setMode(FilesAdapter.MODE_SELECTION); mFilesAdapter.dispatchItemSelected(position); } /** */ @Override protected void onActionModeFinished() { super.onActionModeFinished(); mFilesAdapter.setMode(FilesAdapter.MODE_DEFAULT); } /** */ @Override public boolean onOptionsItemSelected(MenuItem item) { final int itemId = item.getItemId(); switch (itemId) { case R.id.menu_files_action_filter_files: item.setChecked(bFilterFiles = !item.isChecked()); reloadCurrentFiles(); break; case R.id.menu_files_action_filter_directories: item.setChecked(bFilterDirectories = !item.isChecked()); reloadCurrentFiles(); break; case R.id.ex_menu_action_new: final NewFileDialog newFileDialog = new NewFileDialog(); newFileDialog.setTitle(obtainString(R.string.Storage_Files_Dialog_File_New_Title)); newFileDialog.setNegative(obtainString(R.string.Storage_Files_Dialog_File_New_Button_Negative)); newFileDialog.setPositive(obtainString(R.string.Storage_Files_Dialog_File_New_Button_Positive)); newFileDialog.show(this); return true; case R.id.ex_menu_action_discard: this.setLoadingVisible(true); mStorageAssistant.discardFiles(mFilesAdapter.getSelectedItemPaths()); getActionMode().finish(); return true; case R.id.ex_menu_action_copy: case R.id.ex_menu_action_move: mStorageAssistant.updateClipboard(itemId, mFilesAdapter.getSelectedItemPaths()); getActionMode().finish(); invalidateOptionsMenu(); this.setNavigationButtonsVisible(true); // todo: change right button text depends on the selected action return true; } return super.onOptionsItemSelected(item); } /** */ @Override public void onStorageResult(int action, Storage.BaseResult result) { Log.d(TAG, "onStorageResult() message: " + result.message); if (result.isError) { switch (result.errorCode) { case Storage.ERROR_FILE_SAME_AS_DIRECTORY: case Storage.ERROR_DIRECTORY_SAME_AS_FILE: Toast.makeText(getActivity(), "IO error occurred", Toast.LENGTH_SHORT).show(); break; case Storage.ERROR_IO: Toast.makeText(getActivity(), result.message, Toast.LENGTH_SHORT).show(); break; case Storage.ERROR_UNSUPPORTED_OPERATION: Toast.makeText(getActivity(), "This operation is not supported", Toast.LENGTH_SHORT).show(); break; case Storage.ERROR_API: Toast.makeText(getActivity(), "Error due to API limitations", Toast.LENGTH_SHORT).show(); break; default: Toast.makeText(getActivity(), "Unhandled storage error: " + Integer.toString(result.errorCode), Toast.LENGTH_SHORT).show(); } } this.reloadCurrentFiles(); } /** */ @Override public void onFilesLoaded(List<File> files) { mTextCurrentPath.setText(mCurrentFilesPath + File.separator); if (mFilesAdapter != null) { mFilesAdapter.changeItems(files); } if (mCurrentFilesPath.startsWith(EXTERNAL_PACKAGE_DIR_PATH)) { if (!bInExternalPackageSection) { this.bInExternalPackageSection = true; invalidateOptionsMenu(); } } else if (bInExternalPackageSection) { this.bInExternalPackageSection = false; invalidateOptionsMenu(); } this.setLoadingVisible(false); } /** */ @Override public void onDialogButtonClick(ExSimpleDialog dialog, int button) { dialog.dismiss(); switch (button) { case NewFileDialog.POSITIVE: final NewFileDialog fileDialog = (NewFileDialog) dialog; final boolean asDir = fileDialog.isCreateFileAsDirectoryChecked(); final String fileName = fileDialog.getNameOfNewFile(); if (!TextUtils.isEmpty(fileName)) { final String filePath = mCurrentFilesPath + File.separator + fileName; if (asDir) { if (StorageUtils.directoryExists(filePath)) { Toast.makeText(getActivity(), "Directory already exists", Toast.LENGTH_SHORT).show(); return; } STORAGE.createDirectory(filePath); } else { if (StorageUtils.fileExists(filePath)) { Toast.makeText(getActivity(), "File already exists", Toast.LENGTH_SHORT).show(); return; } STORAGE.createFile(filePath); } this.reloadCurrentFiles(); } break; } } /** */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Bundle args = getArguments(); if (args == null || !args.containsKey(BUNDLE_FILES_PATH)) { throw new IllegalStateException("No files initial path found within arguments."); } setHasOptionsMenu(true); this.mStorageAssistant = new StorageAssistant(getActivity()); mStorageAssistant.setStorageWatcher(this); this.mNavigationButtons = new ExNavigationButtons(getActivity()); mNavigationButtons.setOnButtonsListener(this); // Set up adapter. setAdapter(mFilesAdapter = new FilesAdapter(getActivity())); // Load files for initial path. this.loadFilesForPath(args.getString(BUNDLE_FILES_PATH), true); } /** */ @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); if (bInExternalPackageSection && !mStorageAssistant.isInClipboardMode()) { inflater.inflate(R.menu.menu_files, menu); final SearchView searchView = (SearchView) menu.findItem(R.id.ex_menu_action_search).getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { /** */ @Override public boolean onQueryTextSubmit(String query) { return false; } /** */ @Override public boolean onQueryTextChange(String newText) { mSearchQuery = newText; reloadCurrentFiles(); return true; } }); } } /** */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View layout = inflater.inflate(R.layout.fragment_storage_files, container, false); if (layout != null) { final ViewGroup listContainer = (ViewGroup) layout.findViewById(R.id.fragment_storage_files_layout_list); if (listContainer != null) { final View listLayout = super.onCreateView(inflater, container, savedInstanceState); if (listLayout != null) { listContainer.addView(listLayout); } } } return layout; } /** */ @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ((HomeActivity) getActivity()).setNavigationVisible(false); mLoadingView.setOnTouchListener(new View.OnTouchListener() { /** */ @Override public boolean onTouch(View v, MotionEvent event) { return mLoadingView.getAlpha() > 0; } }); } /** */ @Override public void onDestroyView() { super.onDestroyView(); setNavigationButtonsVisible(false); } /** */ @Override protected boolean onViewClick(View view, int id) { switch (id) { case R.id.fragment_storage_files_text_view_current_path: return onBackPressed(); } return super.onViewClick(view, id); } /** */ @Override protected boolean onBackPressed() { if (mFilesPathBackStack.isEmpty()) { return super.onBackPressed(); } final String path = mFilesPathBackStack.poll(); this.loadFilesForPath(path, false); return true; } /** * @param visible */ private void setLoadingVisible(boolean visible) { if (mLoadingView != null) { mLoadingView.animate().alpha(visible ? 1 : 0).setDuration(300).start(); } } /** * @param visible */ public void setNavigationButtonsVisible(boolean visible) { final ActionBar actionBar = getActionBar(); if (actionBar != null) { actionBar.setDisplayOptions(visible ? 0 : ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE); actionBar.setCustomView(visible ? mNavigationButtons : null); actionBar.setDisplayShowCustomEnabled(visible); } } /** * */ private void reloadCurrentFiles() { this.loadFilesForPath(mCurrentFilesPath, false); } /** * @param path * @param addToBackStack */ private void loadFilesForPath(String path, boolean addToBackStack) { if (addToBackStack && mCurrentFilesPath != null) { mFilesPathBackStack.addFirst(mCurrentFilesPath); } this.setLoadingVisible(true); mStorageAssistant.loadFiles(mCurrentFilesPath = path, FILTER, NAME_FILTER, this); } }