2015-10-26 11:21:49 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Martijn Brekhof. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.xbmc.kore.ui;
|
|
|
|
|
2016-05-25 18:18:25 +02:00
|
|
|
import android.Manifest;
|
2015-10-26 11:21:49 +01:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.ServiceConnection;
|
2016-05-25 18:18:25 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2015-10-26 11:21:49 +01:00
|
|
|
import android.os.Bundle;
|
2016-05-25 18:18:25 +02:00
|
|
|
import android.support.annotation.NonNull;
|
2015-10-26 11:21:49 +01:00
|
|
|
import android.support.annotation.Nullable;
|
2016-05-25 18:18:25 +02:00
|
|
|
import android.support.v13.app.FragmentCompat;
|
2015-10-26 11:21:49 +01:00
|
|
|
import android.support.v4.app.Fragment;
|
2016-05-25 18:18:25 +02:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2015-10-26 11:21:49 +01:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
|
|
|
import android.view.LayoutInflater;
|
2015-11-12 09:54:19 +01:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2015-10-26 11:21:49 +01:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.xbmc.kore.R;
|
2016-01-26 13:24:01 +01:00
|
|
|
import org.xbmc.kore.Settings;
|
2015-10-26 11:21:49 +01:00
|
|
|
import org.xbmc.kore.host.HostInfo;
|
|
|
|
import org.xbmc.kore.host.HostManager;
|
|
|
|
import org.xbmc.kore.jsonrpc.ApiException;
|
|
|
|
import org.xbmc.kore.jsonrpc.event.MediaSyncEvent;
|
2016-04-21 10:27:49 +02:00
|
|
|
import org.xbmc.kore.service.library.LibrarySyncService;
|
|
|
|
import org.xbmc.kore.service.library.SyncUtils;
|
2015-10-26 11:21:49 +01:00
|
|
|
import org.xbmc.kore.utils.LogUtils;
|
|
|
|
import org.xbmc.kore.utils.UIUtils;
|
2016-05-25 18:18:25 +02:00
|
|
|
import org.xbmc.kore.utils.Utils;
|
2015-10-26 11:21:49 +01:00
|
|
|
|
2016-01-26 13:24:01 +01:00
|
|
|
import butterknife.OnClick;
|
|
|
|
import butterknife.Optional;
|
2015-10-26 11:21:49 +01:00
|
|
|
import de.greenrobot.event.EventBus;
|
|
|
|
|
|
|
|
abstract public class AbstractDetailsFragment extends Fragment
|
2015-10-26 11:21:49 +01:00
|
|
|
implements SwipeRefreshLayout.OnRefreshListener,
|
|
|
|
SyncUtils.OnServiceListener {
|
2015-10-26 11:21:49 +01:00
|
|
|
private static final String TAG = LogUtils.makeLogTag(AbstractDetailsFragment.class);
|
|
|
|
|
|
|
|
private HostManager hostManager;
|
|
|
|
private HostInfo hostInfo;
|
|
|
|
private EventBus bus;
|
|
|
|
private String syncType;
|
|
|
|
|
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
private ServiceConnection serviceConnection;
|
|
|
|
|
2015-11-25 00:33:05 +01:00
|
|
|
// Used to hide the refresh animation when a silent refresh is issued
|
|
|
|
private boolean silentRefresh = false;
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
abstract protected View createView(LayoutInflater inflater, ViewGroup container);
|
|
|
|
|
|
|
|
/**
|
2016-04-21 10:27:49 +02:00
|
|
|
* Should return {@link LibrarySyncService} SyncType that
|
2015-10-26 11:21:49 +01:00
|
|
|
* this fragment initiates
|
2016-04-21 10:27:49 +02:00
|
|
|
* @return {@link LibrarySyncService} SyncType
|
2015-10-26 11:21:49 +01:00
|
|
|
*/
|
|
|
|
abstract protected String getSyncType();
|
|
|
|
|
|
|
|
/**
|
2016-04-21 10:27:49 +02:00
|
|
|
* Should return the {@link LibrarySyncService} syncID if this fragment
|
2015-10-26 11:21:49 +01:00
|
|
|
* synchronizes a single item. The itemId that should be synced must returned by {@link #getSyncItemID()}
|
2016-04-21 10:27:49 +02:00
|
|
|
* @return {@link LibrarySyncService} SyncID
|
2015-10-26 11:21:49 +01:00
|
|
|
*/
|
|
|
|
abstract protected String getSyncID();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should return the item ID for SyncID returned by {@link #getSyncID()}
|
|
|
|
* @return -1 if not used.
|
|
|
|
*/
|
|
|
|
abstract protected int getSyncItemID();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should return the SwipeRefreshLayout if the fragment's view uses one.
|
|
|
|
* Used to notify the user if a sync for synctype returned by {@link #getSyncType()}
|
|
|
|
* is currently in progress
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
abstract protected SwipeRefreshLayout getSwipeRefreshLayout();
|
|
|
|
|
2016-01-26 13:24:01 +01:00
|
|
|
/**
|
|
|
|
* When the view created in {@link #createView(LayoutInflater, ViewGroup)} contains a button
|
|
|
|
* with resource identifier R.id.download this will be called to initiate the download
|
|
|
|
*/
|
|
|
|
abstract protected void onDownload();
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
@Override
|
|
|
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
bus = EventBus.getDefault();
|
|
|
|
hostManager = HostManager.getInstance(getActivity());
|
|
|
|
hostInfo = hostManager.getHostInfo();
|
|
|
|
syncType = getSyncType();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
if (container == null) {
|
|
|
|
// We're not being shown or there's nothing to show
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
View view = createView(inflater, container);
|
|
|
|
if( view != null ) {
|
|
|
|
swipeRefreshLayout = getSwipeRefreshLayout();
|
|
|
|
if( swipeRefreshLayout != null ) {
|
|
|
|
swipeRefreshLayout.setOnRefreshListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2015-11-12 09:54:19 +01:00
|
|
|
@Override
|
|
|
|
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
|
|
|
setHasOptionsMenu(true);
|
|
|
|
}
|
|
|
|
|
2015-11-13 11:33:58 +01:00
|
|
|
@Override
|
2015-10-26 11:21:49 +01:00
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
serviceConnection = SyncUtils.connectToLibrarySyncService(getActivity(), this);
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
bus.register(this);
|
|
|
|
super.onResume();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
bus.unregister(this);
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
2015-11-13 11:33:58 +01:00
|
|
|
@Override
|
2015-10-26 11:21:49 +01:00
|
|
|
public void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
SyncUtils.disconnectFromLibrarySyncService(getActivity(), serviceConnection);
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
@Override
|
2015-11-12 09:54:19 +01:00
|
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
|
inflater.inflate(R.menu.refresh_item, menu);
|
|
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
2015-11-13 08:41:57 +01:00
|
|
|
switch (item.getItemId()) {
|
2015-11-12 09:54:19 +01:00
|
|
|
case R.id.action_refresh:
|
|
|
|
onRefresh();
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
2015-10-26 11:21:49 +01:00
|
|
|
}
|
|
|
|
|
2016-01-26 13:24:01 +01:00
|
|
|
@Optional
|
|
|
|
@OnClick(R.id.download)
|
|
|
|
public void onDownloadClicked(View v) {
|
2016-05-25 18:18:25 +02:00
|
|
|
boolean hasStoragePermission =
|
|
|
|
ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
|
|
|
|
|
|
|
|
if (!hasStoragePermission) {
|
|
|
|
requestPermissions(new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
|
|
|
Utils.PERMISSION_REQUEST_WRITE_STORAGE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-26 13:24:01 +01:00
|
|
|
if (Settings.allowedDownloadNetworkTypes(getActivity()) != 0) {
|
|
|
|
onDownload();
|
|
|
|
} else {
|
|
|
|
Toast.makeText(getActivity(), R.string.no_connection_type_selected, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 18:18:25 +02:00
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
|
|
|
|
switch (requestCode) {
|
|
|
|
case Utils.PERMISSION_REQUEST_WRITE_STORAGE:
|
|
|
|
// If request is cancelled, the result arrays are empty.
|
|
|
|
if ((grantResults.length > 0) &&
|
|
|
|
(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
|
|
|
|
onDownloadClicked(null);
|
|
|
|
} else {
|
|
|
|
Toast.makeText(getActivity(), R.string.write_storage_permission_denied, Toast.LENGTH_SHORT)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
protected void startSync(boolean silentRefresh) {
|
2015-11-25 00:33:05 +01:00
|
|
|
this.silentRefresh = silentRefresh;
|
|
|
|
LogUtils.LOGD(TAG, "Starting syc. Silent? " + silentRefresh);
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
if (getHostInfo() != null) {
|
2015-11-25 00:33:05 +01:00
|
|
|
if ((swipeRefreshLayout != null) && (!silentRefresh)) {
|
2015-10-26 11:21:49 +01:00
|
|
|
UIUtils.showRefreshAnimation(swipeRefreshLayout);
|
|
|
|
}
|
|
|
|
// Start the syncing process
|
|
|
|
Intent syncIntent = new Intent(this.getActivity(), LibrarySyncService.class);
|
|
|
|
|
2015-11-25 00:33:05 +01:00
|
|
|
if (syncType != null) {
|
2015-10-26 11:21:49 +01:00
|
|
|
syncIntent.putExtra(syncType, true);
|
|
|
|
}
|
2015-11-12 09:54:19 +01:00
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
String syncID = getSyncID();
|
|
|
|
int itemId = getSyncItemID();
|
2015-11-25 00:33:05 +01:00
|
|
|
if ((syncID != null) && (itemId != -1)) {
|
2015-10-26 11:21:49 +01:00
|
|
|
syncIntent.putExtra(syncID, itemId);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bundle syncExtras = new Bundle();
|
|
|
|
syncExtras.putBoolean(LibrarySyncService.SILENT_SYNC, silentRefresh);
|
|
|
|
syncIntent.putExtra(LibrarySyncService.SYNC_EXTRAS, syncExtras);
|
|
|
|
|
|
|
|
getActivity().startService(syncIntent);
|
|
|
|
} else {
|
2015-11-25 00:33:05 +01:00
|
|
|
if (swipeRefreshLayout != null) {
|
2015-10-26 11:21:49 +01:00
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
Toast.makeText(getActivity(), R.string.no_xbmc_configured, Toast.LENGTH_SHORT)
|
2015-11-25 00:33:05 +01:00
|
|
|
.show();
|
2015-10-26 11:21:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Swipe refresh layout callback
|
|
|
|
*/
|
|
|
|
/** {@inheritDoc} */
|
|
|
|
@Override
|
|
|
|
public void onRefresh () {
|
|
|
|
startSync(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event bus post. Called when the syncing process ended
|
|
|
|
*
|
|
|
|
* @param event Refreshes data
|
|
|
|
*/
|
|
|
|
public void onEventMainThread(MediaSyncEvent event) {
|
|
|
|
if ((syncType == null) || (! event.syncType.equals(syncType)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
boolean silentSync = false;
|
|
|
|
if (event.syncExtras != null) {
|
|
|
|
silentSync = event.syncExtras.getBoolean(LibrarySyncService.SILENT_SYNC, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( swipeRefreshLayout != null ) {
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
}
|
|
|
|
onSyncProcessEnded(event);
|
|
|
|
if (event.status == MediaSyncEvent.STATUS_SUCCESS) {
|
|
|
|
if (!silentSync) {
|
|
|
|
Toast.makeText(getActivity(),
|
|
|
|
R.string.sync_successful, Toast.LENGTH_SHORT)
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
} else if (!silentSync) {
|
|
|
|
String msg = (event.errorCode == ApiException.API_ERROR) ?
|
|
|
|
String.format(getString(R.string.error_while_syncing), event.errorMessage) :
|
|
|
|
getString(R.string.unable_to_connect_to_xbmc);
|
|
|
|
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
@Override
|
|
|
|
public void onServiceConnected(LibrarySyncService librarySyncService) {
|
|
|
|
if (syncType == null)
|
|
|
|
return;
|
|
|
|
|
2015-11-25 00:33:05 +01:00
|
|
|
if (!silentRefresh &&
|
|
|
|
(swipeRefreshLayout != null) &&
|
|
|
|
SyncUtils.isLibrarySyncing(librarySyncService,
|
|
|
|
HostManager.getInstance(getActivity()).getHostInfo(),
|
|
|
|
syncType)) {
|
|
|
|
LogUtils.LOGD(TAG, "Showing refresh animation");
|
|
|
|
UIUtils.showRefreshAnimation(swipeRefreshLayout);
|
2015-10-26 11:21:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 11:21:49 +01:00
|
|
|
/**
|
|
|
|
* Called when sync process for type set through {@link #getSyncType()} ends
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
abstract protected void onSyncProcessEnded(MediaSyncEvent event);
|
|
|
|
|
|
|
|
protected HostManager getHostManager() {
|
|
|
|
return hostManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected HostInfo getHostInfo() {
|
|
|
|
return hostInfo;
|
|
|
|
}
|
|
|
|
}
|