music-cyclon/app/src/main/java/max/music_cyclon/SynchronizeActivity.java

259 lines
8.5 KiB
Java

package max.music_cyclon;
import android.Manifest;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import org.json.JSONException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import max.music_cyclon.service.LibraryService;
import max.music_cyclon.slidingtab.SlidingTabLayout;
public class SynchronizeActivity extends AppCompatActivity {
private PagerAdapter pagerAdapter;
/** Messenger for communicating with service. */
Messenger mService = null;
/** Flag indicating whether we have called bind on the service. */
private boolean mIsBound;
/**
* Target we publish for clients to send messages to IncomingHandler.
*/
private final Messenger mMessenger = new Messenger(new IncomingHandler());
private ProgressDialog syncProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_synchronize);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
List<Config> configs = Collections.emptyList();
try {
FileInputStream in = openFileInput("configs.json");
configs = Config.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
pagerAdapter = new PagerAdapter(configs, getSupportFragmentManager());
final ViewPager pager = (ViewPager) findViewById(R.id.container);
assert pager != null;
pager.setAdapter(pagerAdapter);
// Initialize tabs
final SlidingTabLayout tabs = (SlidingTabLayout) findViewById(R.id.tabs);
assert tabs != null;
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.accentColor);
}
});
tabs.setViewPager(pager);
// Update tabs on dataset change
pagerAdapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
tabs.setViewPager(pager);
}
});
View addButton = findViewById(R.id.add_button);
assert addButton != null;
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pagerAdapter.add(UUID.randomUUID().toString().substring(0, 5));
pagerAdapter.notifyDataSetChanged();
}
});
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
0);
}
}
}
@Override
protected void onStop() {
super.onStop();
try {
FileOutputStream fos = openFileOutput("configs.json", Context.MODE_PRIVATE);
getPagerAdapter().save(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
public PagerAdapter getPagerAdapter() {
return pagerAdapter;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Intent preferenceIntent = new Intent(this, MainPreferenceActivity.class);
startActivity(preferenceIntent);
return true;
case R.id.action_sync:
Intent intent = new Intent(SynchronizeActivity.this, LibraryService.class);
List<Config> configs = getPagerAdapter().getConfigData();
intent.putExtra("configs", configs.toArray(new Config[configs.size()]));
SynchronizeActivity.this.startService(intent);
doBindService();
syncProgress = new ProgressDialog(SynchronizeActivity.this);
syncProgress.setMessage("Synchronizing");
syncProgress.setCancelable(false);
syncProgress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
syncProgress.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case LibraryService.MSG_FINISHED:
syncProgress.dismiss();
break;
default:
super.handleMessage(msg);
}
}
}
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mService = new Messenger(service);
try {
Message msg = Message.obtain(null, LibraryService.MSG_REGISTER_CLIENT);
msg.replyTo = mMessenger;
mService.send(msg);
} catch (RemoteException ignored) {
// In this case the service has crashed before we could even
// do anything with it; we can count on soon being
// disconnected (and then reconnected if it can be restarted)
// so there is no need to do anything here.
}
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
};
void doBindService() {
bindService(new Intent(
SynchronizeActivity.this,
LibraryService.class
), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
// If we have received the service, and hence registered with
// it, then now is the time to unregister.
if (mService != null) {
try {
Message msg = Message.obtain(null,
LibraryService.MSG_UNREGISTER_CLIENT);
msg.replyTo = mMessenger;
mService.send(msg);
} catch (RemoteException e) {
// There is nothing special we need to do if the service
// has crashed.
}
}
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
}