Fixed update of double pane view when download in notification bar is pressed in...
[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 private ServiceConnection mConnection = null;
56
57 @Override
58 protected void onCreate(Bundle savedInstanceState) {
59 super.onCreate(savedInstanceState);
60
61 // check if configuration changed to large-land ; for a tablet being changed from portrait to landscape when in FileDetailActivity
62 Configuration conf = getResources().getConfiguration();
63 mConfigurationChangedToLandscape = (conf.orientation == Configuration.ORIENTATION_LANDSCAPE &&
64 (conf.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
65 );
66
67 if (!mConfigurationChangedToLandscape) {
68 mConnection = new DetailsServiceConnection();
69 bindService(new Intent(this, FileDownloader.class), mConnection, Context.BIND_AUTO_CREATE);
70
71 setContentView(R.layout.file_activity_details);
72
73 ActionBar actionBar = getSupportActionBar();
74 actionBar.setDisplayHomeAsUpEnabled(true);
75
76 OCFile file = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE);
77 Account account = getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT);
78 FileDetailFragment mFileDetail = new FileDetailFragment(file, account);
79
80 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
81 ft.replace(R.id.fragment, mFileDetail, FileDetailFragment.FTAG);
82 ft.commit();
83
84 } else {
85 backToDisplayActivity(); // the 'back' won't be effective until this.onStart() and this.onResume() are completed;
86 }
87
88
89 }
90
91
92 /** Defines callbacks for service binding, passed to bindService() */
93 private class DetailsServiceConnection implements ServiceConnection {
94
95 @Override
96 public void onServiceConnected(ComponentName className, IBinder service) {
97 mDownloaderBinder = (FileDownloaderBinder) service;
98 FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
99 if (fragment != null)
100 fragment.updateFileDetails(); // let the fragment gets the mDownloadBinder through getDownloadBinder() (see FileDetailFragment#updateFileDetais())
101 }
102
103 @Override
104 public void onServiceDisconnected(ComponentName arg0) {
105 mDownloaderBinder = null;
106 }
107 };
108
109
110 @Override
111 public void onDestroy() {
112 super.onDestroy();
113 if (mConnection != null) {
114 unbindService(mConnection);
115 mConnection = null;
116 }
117 }
118
119
120 @Override
121 public boolean onOptionsItemSelected(MenuItem item) {
122 boolean returnValue = false;
123
124 switch(item.getItemId()){
125 case android.R.id.home:
126 backToDisplayActivity();
127 returnValue = true;
128 }
129
130 return returnValue;
131 }
132
133
134
135 @Override
136 protected void onResume() {
137
138 super.onResume();
139 if (!mConfigurationChangedToLandscape) {
140 FileDetailFragment fragment = (FileDetailFragment) getSupportFragmentManager().findFragmentByTag(FileDetailFragment.FTAG);
141 fragment.updateFileDetails();
142 }
143 }
144
145
146 private void backToDisplayActivity() {
147 Intent intent = new Intent(this, FileDisplayActivity.class);
148 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
149 intent.putExtra(FileDetailFragment.EXTRA_FILE, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_FILE));
150 intent.putExtra(FileDetailFragment.EXTRA_ACCOUNT, getIntent().getParcelableExtra(FileDetailFragment.EXTRA_ACCOUNT));
151 startActivity(intent);
152 finish();
153 }
154
155
156 @Override
157 protected Dialog onCreateDialog(int id) {
158 Dialog dialog = null;
159 switch (id) {
160 case DIALOG_SHORT_WAIT: {
161 ProgressDialog working_dialog = new ProgressDialog(this);
162 working_dialog.setMessage(getResources().getString(
163 R.string.wait_a_moment));
164 working_dialog.setIndeterminate(true);
165 working_dialog.setCancelable(false);
166 dialog = working_dialog;
167 break;
168 }
169 default:
170 dialog = null;
171 }
172 return dialog;
173 }
174
175
176 /**
177 * {@inheritDoc}
178 */
179 @Override
180 public void onFileStateChanged() {
181 // nothing to do here!
182 }
183
184
185 /**
186 * {@inheritDoc}
187 */
188 @Override
189 public FileDownloaderBinder getFileDownloaderBinder() {
190 return mDownloaderBinder;
191 }
192
193 }