Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/main/java/org/amahi/anywhere/activity/RecentFilesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.Snackbar;
Expand All @@ -18,11 +19,17 @@
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.android.gms.cast.framework.CastButtonFactory;
import com.google.android.gms.cast.framework.CastContext;
import com.google.android.gms.cast.framework.CastState;
import com.google.android.gms.cast.framework.CastStateListener;
import com.google.android.gms.cast.framework.IntroductoryOverlay;
import com.squareup.otto.Subscribe;

import org.amahi.anywhere.AmahiApplication;
Expand Down Expand Up @@ -63,21 +70,26 @@
public class RecentFilesActivity extends AppCompatActivity implements
ServerFileClickListener,
SwipeRefreshLayout.OnRefreshListener,
EasyPermissions.PermissionCallbacks {
EasyPermissions.PermissionCallbacks,
CastStateListener {

@Inject
ServerClient serverClient;
private List<RecentFile> recentFiles;
@FileOption.Types
private int selectedFileOption;
private int selectedPosition = -1;
private CastContext mCastContext;
private MenuItem mediaRouteMenuItem;
private IntroductoryOverlay mIntroductoryOverlay;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recent_files);

setUpInjections();
setUpCast();
setUpHomeNavigation();
setUpFilesContentRefreshing();
}
Expand All @@ -86,6 +98,35 @@ private void setUpInjections() {
AmahiApplication.from(this).inject(this);
}

private void setUpCast() {
mCastContext = CastContext.getSharedInstance(this);
}

@Override
public void onCastStateChanged(int newState) {
if (newState != CastState.NO_DEVICES_AVAILABLE) {
showIntroductoryOverlay();
}
}

private void showIntroductoryOverlay() {
if (mIntroductoryOverlay != null) {
mIntroductoryOverlay.remove();
}
if ((mediaRouteMenuItem != null) && mediaRouteMenuItem.isVisible()) {
new Handler().post(() -> {
mIntroductoryOverlay = new IntroductoryOverlay
.Builder(this, mediaRouteMenuItem)
.setTitleText("Introducing Cast")
.setSingleTime()
.setOnOverlayDismissedListener(
() -> mIntroductoryOverlay = null)
.build();
mIntroductoryOverlay.show();
});
}
}

private void setUpHomeNavigation() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
Expand Down Expand Up @@ -427,8 +468,9 @@ public void onFileDeleteEvent(ServerFileDeleteEvent fileDeleteEvent) {

if (fileDeleteEvent.isDeleted()) {
removeFileFromDatabase(getSelectedRecentFile());
recentFiles.remove(getSelectedRecentFile());
RecentFile recentFile = getSelectedRecentFile();
getListAdapter().removeFile(selectedPosition);
recentFiles.remove(recentFile);
selectedPosition = -1;
} else {
Toast.makeText(this, R.string.message_delete_file_error, Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -488,6 +530,7 @@ private void startDownloadService(ServerFile file) {
protected void onResume() {
super.onResume();

mCastContext.addCastStateListener(this);
BusProvider.getBus().register(this);

setUpRecentFileList();
Expand All @@ -497,10 +540,23 @@ protected void onResume() {
protected void onPause() {
super.onPause();

mCastContext.removeCastStateListener(this);
BusProvider.getBus().unregister(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.action_bar_cast_button, menu);

mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(
this.getApplicationContext(),
menu, R.id.media_route_menu_item);

return true;
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
import org.amahi.anywhere.bus.AudioMetadataRetrievedEvent;
import org.amahi.anywhere.bus.AudioPreparedEvent;
import org.amahi.anywhere.bus.BusProvider;
import org.amahi.anywhere.db.entities.OfflineFile;
import org.amahi.anywhere.db.entities.RecentFile;
import org.amahi.anywhere.db.repositories.OfflineFileRepository;
import org.amahi.anywhere.db.repositories.RecentFileRepository;
import org.amahi.anywhere.fragment.AudioListFragment;
import org.amahi.anywhere.model.AudioMetadata;
Expand Down Expand Up @@ -162,10 +164,14 @@ private void prepareFiles() {
}

private void setUpAudio() {
setUpAudioAdapter();
setUpAudioPosition();
setUpAudioTitle();
setUpAudioListener();
if (mCastSession != null && mCastSession.isConnected()) {
loadRemoteMedia(0, true);
} else {
setUpAudioAdapter();
setUpAudioPosition();
setUpAudioTitle();
setUpAudioListener();
}
}

private void setUpAudioAdapter() {
Expand Down Expand Up @@ -241,7 +247,8 @@ public ServerFilesAudioPageAdapter getAudioAdapter() {
@Subscribe
public void onAudioMetadataRetrieved(AudioMetadataRetrievedEvent event) {
ServerFile audioFile = getFile();
if (audioFile != null && audioFile == event.getServerFile()) {
if (audioFile != null && event.getServerFile() != null &&
audioFile.getUniqueKey().equals(event.getServerFile().getUniqueKey())) {
final AudioMetadata metadata = event.getAudioMetadata();
this.metadataFormatter = new AudioMetadataFormatter(
metadata.getAudioTitle(), metadata.getAudioArtist(), metadata.getAudioAlbum());
Expand Down Expand Up @@ -376,6 +383,7 @@ private List<ServerFile> getFiles() {
public void onAudioPrepared(AudioPreparedEvent event) {
if (areAudioControlsAvailable()) {
audioControls.setMediaPlayer(this);
hideAudioControlsForced();
showAudio();
}
}
Expand Down Expand Up @@ -626,9 +634,11 @@ private void tearDownAudioServiceBind() {
protected void onDestroy() {
super.onDestroy();

if (audioService != null &&
(!audioService.getAudioPlayer().getPlayWhenReady() || getShare() == null)) {
tearDownAudioService();
if(isFinishing()) {
if (audioService != null &&
(!audioService.getAudioPlayer().getPlayWhenReady() || getShare() == null)) {
tearDownAudioService();
}
}
}

Expand Down Expand Up @@ -717,6 +727,7 @@ public void onStatusUpdated() {
Intent intent = new Intent(ServerFileAudioActivity.this, ExpandedControlsActivity.class);
startActivity(intent);
remoteMediaClient.removeListener(this);
finish();
}

@Override
Expand Down Expand Up @@ -755,9 +766,11 @@ private MediaInfo buildMediaInfo() {
audioMetadata.putString(MediaMetadata.KEY_ALBUM_TITLE, metadataFormatter.getAudioAlbum());
} else {
audioMetadata.putString(MediaMetadata.KEY_TITLE, getFile().getNameOnly());
audioMetadata.putString(MediaMetadata.KEY_ARTIST, "");
audioMetadata.putString(MediaMetadata.KEY_ALBUM_TITLE, "");
}

String audioSource = serverClient.getFileUri(getShare(), getFile()).toString();
String audioSource = getRemoteUri(getFile());
MediaInfo.Builder builder = new MediaInfo.Builder(audioSource)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType(getFile().getMime())
Expand All @@ -768,6 +781,16 @@ private MediaInfo buildMediaInfo() {
return builder.build();
}

private String getRemoteUri(ServerFile serverFile) {
OfflineFileRepository repository = new OfflineFileRepository(this);
OfflineFile offlineFile = repository.getOfflineFile(serverFile.getName(), serverFile.getModificationTime().getTime());
if (offlineFile != null) {
return offlineFile.getFileUri();
} else {
return getAudioUri().toString();
}
}

private enum PlaybackLocation {
LOCAL,
REMOTE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,35 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.cast.MediaInfo;
import com.google.android.gms.cast.MediaLoadOptions;
import com.google.android.gms.cast.MediaMetadata;
import com.google.android.gms.cast.framework.CastButtonFactory;
import com.google.android.gms.cast.framework.CastContext;
import com.google.android.gms.cast.framework.CastSession;
import com.google.android.gms.cast.framework.SessionManagerListener;
import com.google.android.gms.cast.framework.media.RemoteMediaClient;
import com.google.android.gms.common.images.WebImage;
import com.squareup.otto.Subscribe;

import org.amahi.anywhere.AmahiApplication;
import org.amahi.anywhere.R;
import org.amahi.anywhere.adapter.ServerFilesImagePagerAdapter;
import org.amahi.anywhere.bus.BusProvider;
import org.amahi.anywhere.bus.FileCopiedEvent;
import org.amahi.anywhere.bus.FileDownloadedEvent;
import org.amahi.anywhere.db.entities.OfflineFile;
import org.amahi.anywhere.db.entities.RecentFile;
import org.amahi.anywhere.db.repositories.OfflineFileRepository;
import org.amahi.anywhere.db.repositories.RecentFileRepository;
import org.amahi.anywhere.fragment.PrepareDialogFragment;
import org.amahi.anywhere.fragment.ServerFileDownloadingFragment;
import org.amahi.anywhere.model.FileOption;
import org.amahi.anywhere.server.client.ServerClient;
Expand Down Expand Up @@ -237,20 +242,17 @@ public void onPageSelected(int position) {
}

private void setUpRecentFiles(ServerFile serverFile) {
String uri;
long size;
if (isFileAvailableOffline(serverFile)) {
uri = getUriFrom(serverFile.getName(), serverFile.getModificationTime());
size = new File(getOfflineFileUri(serverFile.getName())).length();
} else {
uri = getImageUri();
size = serverFile.getSize();
}

String serverName = Preferences.getServerName(this);

RecentFile recentFile = new RecentFile(serverFile.getUniqueKey(),
uri,
getImageUri(serverFile),
serverName,
System.currentTimeMillis(),
size);
Expand Down Expand Up @@ -290,7 +292,7 @@ public boolean onOptionsItemSelected(MenuItem menuItem) {
return true;

case R.id.menu_share:
startFileSharingActivity();
prepareDownload();
return true;

default:
Expand All @@ -302,6 +304,28 @@ private void startFileSharingActivity() {
startFileDownloading(getShare(), getCurrentFile());
}

private void prepareDownload() {

ServerFile serverFile = getCurrentFile();

if (isFileAvailableOffline(serverFile)) {
prepareDownloadingFile(serverFile);
} else {
startFileDownloading(getShare(), getCurrentFile());
}
}

private void prepareDownloadingFile(ServerFile file) {
PrepareDialogFragment fragment = new PrepareDialogFragment();
fragment.show(getSupportFragmentManager(), "prepare_dialog");
FileManager fm = FileManager.newInstance(this);
Uri offlinePath = fm.getContentUriForOfflineFile(file.getName());
File sourceLocation = new File(offlinePath.toString());
File downloadLocation = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), file.getName());

fm.copyFile(sourceLocation, downloadLocation);
}

private ServerFile getCurrentFile() {
return getImageFiles().get(getImagePager().getCurrentItem());
}
Expand Down Expand Up @@ -329,6 +353,21 @@ private void startFileSharingActivity(ServerFile file, Uri fileUri) {
startActivity(intent);
}

@Subscribe
public void onFileCopied(FileCopiedEvent event) {
Uri contentUri = FileManager.newInstance(this).getContentUri(event.getTargetLocation());

dismissPreparingDialog();
finishFileDownloading(contentUri);
}

private void dismissPreparingDialog() {
PrepareDialogFragment fragment = (PrepareDialogFragment) getSupportFragmentManager().findFragmentByTag("prepare_dialog");
if (fragment != null && fragment.isAdded()) {
fragment.dismiss();
}
}

@Override
protected void onResume() {
super.onResume();
Expand Down Expand Up @@ -400,28 +439,41 @@ private void onApplicationDisconnected() {
private void loadRemoteMedia() {
final RemoteMediaClient remoteMediaClient = mCastSession.getRemoteMediaClient();
if (remoteMediaClient != null) {
remoteMediaClient.load(buildMediaInfo());

MediaLoadOptions mediaLoadOptions = new MediaLoadOptions.Builder().build();
remoteMediaClient.load(buildMediaInfo(), mediaLoadOptions);
}
}

private MediaInfo buildMediaInfo() {
ServerFile file = getImageFiles().get(imagePosition);
MediaMetadata imageMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_PHOTO);
imageMetadata.putString(MediaMetadata.KEY_TITLE, file.getNameOnly());
String imageUrl = getImageUri();
imageMetadata.putString(MediaMetadata.KEY_ARTIST, "");
imageMetadata.putString(MediaMetadata.KEY_ALBUM_TITLE, "");
String imageUrl = getImageUri(getCurrentFile());
imageMetadata.addImage(new WebImage(Uri.parse(imageUrl)));
imageMetadata.addImage(new WebImage(Uri.parse(imageUrl)));
return new MediaInfo.Builder(imageUrl)
.setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
.setContentType(file.getMime())
.setMetadata(imageMetadata)
.build();
}

private String getImageUri() {
private String getImageUri(ServerFile serverFile) {
int fileType = getIntent().getIntExtra(Intents.Extras.FILE_TYPE, FileManager.SERVER_FILE);

if (fileType == FileManager.RECENT_FILE) {
return getRecentFileUri();
}
if (getShare() == null) {
OfflineFileRepository repository = new OfflineFileRepository(this);
OfflineFile offlineFile = repository.getOfflineFile(serverFile.getName(), serverFile.getModificationTime().getTime());
if (offlineFile != null) {
return offlineFile.getFileUri();
}
}
return serverClient.getFileUri(getShare(), getCurrentFile()).toString();
}

Expand Down
Loading