Download service refactoring: multiple downloads and cancellation support
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / activity / FileDetailActivity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2011 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18 package com.owncloud.android.ui.activity;
19
20 import android.accounts.Account;
21 import android.app.Dialog;
22 import android.app.ProgressDialog;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.ServiceConnection;
27 import android.content.res.Configuration;
28 import android.os.Bundle;
29 import android.os.IBinder;
30 import android.support.v4.app.FragmentTransaction;
31
32 import com.actionbarsherlock.app.ActionBar;
33 import com.actionbarsherlock.app.SherlockFragmentActivity;
34 import com.actionbarsherlock.view.MenuItem;
35 import com.owncloud.android.datamodel.OCFile;
36 import com.owncloud.android.files.services.FileDownloader;
37 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
38 import com.owncloud.android.ui.fragment.FileDetailFragment;
39
40 import com.owncloud.android.R;
41
42 /**
43 * This activity displays the details of a file like its name, its size and so
44 * on.
45 *
46 * @author Bartek Przybylski
47 *
48 */
49 public class FileDetailActivity extends SherlockFragmentActivity implements FileDetailFragment.ContainerActivity {
50
51 public static final int DIALOG_SHORT_WAIT = 0;
52
53 private boolean mConfigurationChangedToLandscape = false;
54 private FileDownloaderBinder mDownloaderBinder = null;
55
56 @Override
57 protected void onCreate(Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
59
60 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
61 Configuration conf = getResources().getConfiguration();
62 mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
63 (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
64 );
65
66 if (!mConfigurationChangedToLandscape) {
67 bindService(new Intent(this, FileDownloader.class), mConnection, Context.BIND_AUTO_CREATE);
68
69 setContentView(R.layout.file_activity_details);
70
71 ActionBar actionBar = getSupportActionBar();
72 actionBar.setDisplayHomeAsUpEnabled(true);
73
74 OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
75 Account account = getIntent().getParcelableExtra(FileDownloader.EXTRA_ACCOUNT);
76 FileDetailFragment mFileDetail = new FileDetailFragment(file, account);
77
78 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
79 ft.replace(R.id.fragment, mFileDetail, FileDetailFragment.FTAG);
80 ft.commit();
81
82 } else {
83 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
84 }
85
86
87 }
88
89
90 /** Defines callbacks for service binding, passed to bindService() */
91 private ServiceConnection mConnection = new ServiceConnection() {
92
93 @Override
94 public void onServiceConnected(ComponentName className, IBinder service) {
95 mDownloaderBinder = (FileDownloaderBinder) service;
96 FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
97 if (fragment != null)
98 fragment.updateFileDetails(); // a new chance to get the mDownloadBinder through getDownloadBinder()
99 }
100
101 @Override
102 public void onServiceDisconnected(ComponentName arg0) {
103 mDownloaderBinder = null;
104 }
105 };
106
107
108 @Override
109 public void onDestroy() {
110 super.onDestroy();
111 unbindService(mConnection);
112 }
113
114
115 @Override
116 public boolean onOptionsItemSelected(MenuItem item) {
117 boolean returnValue = false;
118
119 switch(item.getItemId()){
120 case android.R.id.home:
121 backToDisplayActivity();
122 returnValue = true;
123 }
124
125 return returnValue;
126 }
127
128
129
130 @Override
131 protected void onResume() {
132
133 super.onResume();
134 if (!mConfigurationChangedToLandscape) {
135 FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
136 fragment.updateFileDetails();
137 }
138 }
139
140
141 private void backToDisplayActivity() {
142 Intent intent = new Intent(this, FileDisplayActivity.class);
143 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
144 intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE));
145 startActivity(intent);
146 finish();
147 }
148
149
150 @Override
151 protected Dialog onCreateDialog(int id) {
152 Dialog dialog = null;
153 switch (id) {
154 case DIALOG_SHORT_WAIT: {
155 ProgressDialog working_dialog = new ProgressDialog(this);
156 working_dialog.setMessage(getResources().getString(
157 R.string.wait_a_moment));
158 working_dialog.setIndeterminate(true);
159 working_dialog.setCancelable(false);
160 dialog = working_dialog;
161 break;
162 }
163 default:
164 dialog = null;
165 }
166 return dialog;
167 }
168
169
170 /**
171 * {@inheritDoc}
172 */
173 @Override
174 public void onFileStateChanged() {
175 // nothing to do here!
176 }
177
178
179 /**
180 * {@inheritDoc}
181 */
182 @Override
183 public FileDownloaderBinder getFileDownloaderBinder() {
184 return mDownloaderBinder;
185 }
186
187 }