-/* ownCloud Android client application\r
- * Copyright (C) 2011 Bartek Przybylski\r
- *\r
- * This program is free software: you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation, either version 3 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.\r
- *\r
- */\r
-package eu.alefzero.owncloud.ui.fragment;\r
-\r
-import android.app.FragmentTransaction;\r
-import android.content.BroadcastReceiver;\r
-import android.content.Context;\r
-import android.content.Intent;\r
-import android.content.IntentFilter;\r
-import android.graphics.Bitmap;\r
-import android.graphics.BitmapFactory;\r
-import android.os.Bundle;\r
-import android.view.LayoutInflater;\r
-import android.view.View;\r
-import android.view.View.OnClickListener;\r
-import android.view.ViewGroup;\r
-import android.widget.Button;\r
-import android.widget.ImageView;\r
-import android.widget.TextView;\r
-import android.widget.Toast;\r
-\r
-import com.actionbarsherlock.app.SherlockFragment;\r
-\r
-import eu.alefzero.owncloud.DisplayUtils;\r
-import eu.alefzero.owncloud.FileDownloader;\r
-import eu.alefzero.owncloud.R;\r
-import eu.alefzero.owncloud.datamodel.OCFile;\r
-\r
-/**\r
- * This Fragment is used to display the details about a file.\r
- * \r
- * @author Bartek Przybylski\r
- * \r
- */\r
-public class FileDetailFragment extends SherlockFragment implements\r
- OnClickListener {\r
-\r
- public static final String FILE = "FILE";\r
-\r
- private Intent mIntent;\r
- //private View mView;\r
- private DownloadFinishReceiver mDownloadFinishReceiver;\r
- private OCFile mFile;\r
-\r
- private int mLayout;\r
- private boolean mEmptyLayout;\r
-\r
- /**\r
- * Default constructor. When inflated by android -> display empty layout\r
- */\r
- public FileDetailFragment() {\r
- mLayout = R.layout.file_details_empty;\r
- mEmptyLayout = true;\r
- }\r
-\r
- /**\r
- * Custom construtor. Use with a {@link FragmentTransaction}. The intent has\r
- * to contain {@link FileDetailFragment#FILE} with an OCFile and also\r
- * {@link FileDownloader#EXTRA_ACCOUNT} with the account.\r
- * \r
- * @param intent Intent with an account and a file in it for rendering\r
- */\r
- public FileDetailFragment(Intent intent) {\r
- mLayout = R.layout.file_details_fragment;\r
- mIntent = intent;\r
- mEmptyLayout = false;\r
- }\r
-\r
- @Override\r
- public void onResume() {\r
- super.onResume();\r
- mDownloadFinishReceiver = new DownloadFinishReceiver();\r
- IntentFilter filter = new IntentFilter(\r
- FileDownloader.DOWNLOAD_FINISH_MESSAGE);\r
- getActivity().registerReceiver(mDownloadFinishReceiver, filter);\r
- }\r
-\r
- @Override\r
- public void onPause() {\r
- super.onPause();\r
- getActivity().unregisterReceiver(mDownloadFinishReceiver);\r
- mDownloadFinishReceiver = null;\r
- }\r
-\r
- /**\r
- * Use this method to signal this Activity that it shall update its view.\r
- * \r
- * @param intent The {@link Intent} that contains extra information about\r
- * this file The intent needs to have these extras:\r
- * <p>\r
- * \r
- * {@link FileDetailFragment#FILE}: An {@link OCFile}\r
- * {@link FileDownloader#EXTRA_ACCOUNT}: The Account that file\r
- * belongs to (required for downloading)\r
- */\r
- public void updateFileDetails(Intent intent) {\r
- mIntent = intent;\r
- updateFileDetails();\r
- }\r
-\r
- private void updateFileDetails() {\r
- mFile = mIntent.getParcelableExtra(FILE);\r
-\r
- if (mFile != null) {\r
- // set file details\r
- setFilename(mFile.getFileName());\r
- setFiletype(DisplayUtils.convertMIMEtoPrettyPrint(mFile\r
- .getMimetype()));\r
- setFilesize(mFile.getFileLength());\r
- \r
- // Update preview\r
- if (mFile.getStoragePath() != null) {\r
- if (mFile.getMimetype().startsWith("image/")) {\r
- ImageView preview = (ImageView) getView().findViewById(\r
- R.id.fdPreview);\r
- Bitmap bmp = BitmapFactory.decodeFile(mFile.getStoragePath());\r
- preview.setImageBitmap(bmp);\r
- }\r
- }\r
- \r
- // Make download button effective\r
- Button downloadButton = (Button) getView().findViewById(R.id.fdDownloadBtn);\r
- downloadButton.setOnClickListener(this);\r
- }\r
- }\r
-\r
- @Override\r
- public View onCreateView(LayoutInflater inflater, ViewGroup container,\r
- Bundle savedInstanceState) {\r
- View view = null;\r
- view = inflater.inflate(mLayout, container, false);\r
- return view;\r
- }\r
-\r
- @Override\r
- public void onStart() {\r
- super.onStart();\r
- \r
- // Fill in required information about file displaying\r
- if(mIntent == null){\r
- mIntent = getActivity().getIntent();\r
- }\r
- \r
- // Fill in the details if the layout is not empty\r
- if(!mEmptyLayout){\r
- updateFileDetails();\r
- }\r
- \r
- }\r
-\r
- private void setFilename(String filename) {\r
- TextView tv = (TextView) getView().findViewById(R.id.fdFilename);\r
- if (tv != null)\r
- tv.setText(filename);\r
- }\r
-\r
- private void setFiletype(String mimetype) {\r
- TextView tv = (TextView) getView().findViewById(R.id.fdType);\r
- if (tv != null)\r
- tv.setText(mimetype);\r
- }\r
-\r
- private void setFilesize(long filesize) {\r
- TextView tv = (TextView) getView().findViewById(R.id.fdSize);\r
- if (tv != null)\r
- tv.setText(DisplayUtils.bitsToHumanReadable(filesize));\r
- }\r
-\r
- /**\r
- * Use this to check if the correct layout is loaded. When android\r
- * instanciates this class using the default constructor, the layout will be\r
- * empty.\r
- * \r
- * Once a user touches a file for the first time, you must instanciate a new\r
- * Fragment with the new FileDetailFragment(true) to inflate the actual\r
- * details\r
- * \r
- * @return If the layout is empty, this method will return true, otherwise\r
- * false\r
- */\r
- public boolean isEmptyLayout() {\r
- return mEmptyLayout;\r
- }\r
-\r
- @Override\r
- public void onClick(View v) {\r
- Toast.makeText(getActivity(), "Downloading", Toast.LENGTH_LONG).show();\r
- Intent i = new Intent(getActivity(), FileDownloader.class);\r
- i.putExtra(FileDownloader.EXTRA_ACCOUNT,\r
- mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT));\r
- i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getPath());\r
- getActivity().startService(i);\r
- }\r
-\r
- private class DownloadFinishReceiver extends BroadcastReceiver {\r
- @Override\r
- public void onReceive(Context context, Intent intent) {\r
- updateFileDetails();\r
- }\r
-\r
- }\r
-\r
-}\r