Skip to content

Commit

Permalink
Merge pull request #5 from IRA-Team/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hluhovskyi committed Oct 26, 2015
2 parents 3cb1788 + ff61bc1 commit 89cb536
Show file tree
Hide file tree
Showing 34 changed files with 344 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ protected void onResume() {
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.menu_audio, menu);
setCacheAction(playerService.getPlayingAudio().isCached());
if (playerService != null) {
Audio audio = playerService.getPlayingAudio();
if (audio != null) {
setCacheAction(audio.isCached());
}
}
return true;
}

Expand Down Expand Up @@ -129,6 +134,11 @@ public void onServiceConnected(ComponentName name, IBinder service) {
playerService = ((PlayerService.PlayerBinder) service).getPlayerService();
playerController.setPlayerService(playerService);
playerService.addPlayerEventListener(playerController);

Audio audio = playerService.getPlayingAudio();
if (audio != null) {
setCacheAction(audio.isCached());
}
}

@Override
Expand All @@ -143,12 +153,14 @@ public void finish() {
}

public void setCacheAction(boolean isCached) {
if (isCached) {
menu.findItem(R.id.action_remove_from_cache).setVisible(true);
menu.findItem(R.id.action_cache).setVisible(false);
} else {
menu.findItem(R.id.action_remove_from_cache).setVisible(false);
menu.findItem(R.id.action_cache).setVisible(true);
if (menu != null) {
if (isCached) {
menu.findItem(R.id.action_remove_from_cache).setVisible(true);
menu.findItem(R.id.action_cache).setVisible(false);
} else {
menu.findItem(R.id.action_remove_from_cache).setVisible(false);
menu.findItem(R.id.action_cache).setVisible(true);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected void onCreate(Bundle savedInstanceState) {
new UserService(this).getCurrentUser((user) -> {
ImageLoader.getInstance().displayImage(user.photo_100, roundImageView);
userFullName.setText(user.first_name + " " + user.last_name);
userVkId.setText(String.valueOf(user.id));
userVkId.setText(UserService.USER_LINK + String.valueOf(user.id));
});

ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
Expand Down Expand Up @@ -419,13 +419,22 @@ public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
MenuItem menuItem = navigationView.getMenu().findItem(R.id.current_playlist);
switch (item.getItemId()) {
case R.id.action_play:
playerService.setPlaylist(audioAdapter.getCheckedItems());
playerService.play(0);
menuItem.setChecked(true);
onNavigationItemSelected(menuItem);
break;

case R.id.action_cache:
Intent intent = new Intent(this, DownloadService.class);
intent.setAction(DownloadService.START_DOWNLOADING);
intent.putExtra(DownloadService.AUDIO_LIST, (ArrayList<Audio>) audioAdapter.getCheckedItems());
startService(intent);
break;

case R.id.action_remove_from_cache:
List<Audio> list = audioAdapter.getCachedCheckedItems();
audioService.removeFromCache(list, new AudioService.Listener() {
Expand All @@ -440,9 +449,23 @@ public void onError(String errorMessage) {
}
});
break;

case R.id.action_delete:
audioAdapter.removeChecked();
break;

case R.id.action_add_to_playlist:
List<Audio> checked = audioAdapter.getCheckedItems();
List<Audio> playlist = playerService.getPlaylist();
playlist.addAll(checked);
audioAdapter.notifyDataSetChanged();
Snackbar.make(coordinatorLayout, R.string.snackbar_add_to_playlist, Snackbar.LENGTH_LONG)
.setAction(R.string.snackbar_cancel, (v -> {
for (int i = 0; i < checked.size(); i++)
playlist.remove(playlist.size() - 1);
}))
.show();
break;
}
mode.finish();
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void onCreate(Bundle savedInstanceState) {
findPreference("sync_button").setOnPreferenceClickListener(preference -> {
context = getActivity();
Intent intent = new Intent(context, DownloadService.class);
intent.putExtra(DownloadService.USER_SYNC, true);
intent.setAction(DownloadService.START_SYNC);
context.startService(intent);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import com.irateam.vkplayer.services.PlayerService;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;

public class ActivityPlayerController extends PlayerController implements Player.PlayerEventListener {
Expand Down Expand Up @@ -80,7 +78,7 @@ public void setAudio(int position, Audio audio) {
if (!audio.url.startsWith("https://") && !audio.url.startsWith("http://")) {
sizeAudio.setText("SIZE");
} else {
new SizeTask(audio.url).execute();
new SizeTask(audio).execute();
}
}
}
Expand All @@ -101,34 +99,38 @@ public void onProgressChanged(int milliseconds) {
));
}

class SizeTask extends AsyncTask<Void, Void, Double> {
class SizeTask extends AsyncTask<Void, Void, Long> {

private String url;
private Audio audio;

public SizeTask(String url) {
this.url = url;
public SizeTask(Audio audio) {
this.audio = audio;
}

@Override
protected Double doInBackground(Void... params) {
protected void onPreExecute() {
super.onPreExecute();
}

URLConnection urlConnection;
double size = 0;
@Override
protected Long doInBackground(Void... params) {
long size = 0;
try {

urlConnection = new URL(url).openConnection();
urlConnection.connect();
size = urlConnection.getContentLength() / (double) 1024 / (double) 1024;
size = audio.getSize();
} catch (IOException e) {
e.printStackTrace();
}
return size;
}

@Override
protected void onPostExecute(Double size) {
protected void onPostExecute(Long size) {
super.onPostExecute(size);
sizeAudio.setText(String.format("%.1f", size) + "Mb");
if (size > 0) {
sizeAudio.setText(String.format("%.1f", size / (double) 1024 / (double) 1024) + "Mb");
} else {
sizeAudio.setText("");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,23 @@ public AudioDatabaseHelper(Context context) {

public long insert(Audio audio) {
SQLiteDatabase db = getWritableDatabase();
return db.insert(TABLE_NAME, null, toContentValues(audio));
long id = db.insert(TABLE_NAME, null, toContentValues(audio));
db.close();
return id;
}

public long update(Audio audio) {
SQLiteDatabase db = getWritableDatabase();
return db.update(TABLE_NAME, toContentValues(audio), "id = " + audio.id, null);
int id = db.update(TABLE_NAME, toContentValues(audio), "id = " + audio.id, null);
db.close();
return id;
}

public long delete(Audio audio) {
SQLiteDatabase db = getWritableDatabase();
return db.delete(TABLE_NAME, "id = " + audio.id, null);
int id = db.delete(TABLE_NAME, "id = " + audio.id, null);
db.close();
return id;
}

public void delete(List<Audio> list) {
Expand All @@ -60,19 +66,23 @@ public long cache(Audio audio) {
} else {
id = update(audio);
}
cursor.close();
db.close();
return id;
}

public List<Audio> getAll() {
SQLiteDatabase db = getReadableDatabase();
List<Audio> list = new ArrayList<>();

Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, ID);
if (cursor.moveToFirst()) {
do {
list.add(fromCursor(cursor));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return list;
}

Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/irateam/vkplayer/models/Audio.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.irateam.vkplayer.models;

import android.content.ContentValues;
import android.os.Parcel;

import com.vk.sdk.api.model.VKApiAudio;

import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Audio extends VKApiAudio {
public String cachePath;
Expand Down Expand Up @@ -42,6 +43,14 @@ public Audio(Parcel in) {
this.cachePath = in.readString();
}

public Long getSize() throws IOException {
if (isCached()) {
return new File(getPlayingUrl()).length();
} else {
return (long) new URL(url).openConnection().getContentLength();
}
}

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
Expand All @@ -57,4 +66,6 @@ public Audio[] newArray(int size) {
return new Audio[size];
}
};


}
13 changes: 12 additions & 1 deletion app/src/main/java/com/irateam/vkplayer/models/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.irateam.vkplayer.player.Player;
import com.irateam.vkplayer.services.DownloadService;
Expand All @@ -24,6 +23,7 @@ public class Settings {
public static final String SYNC_ENABLED = "sync_enabled";
public static final String SYNC_TIME = "sync_time";
public static final String SYNC_COUNT = "sync_count";
public static final String SYNC_WIFI = "sync_wifi";

private static final int SYNC_ALARM_ID = 1;

Expand Down Expand Up @@ -72,6 +72,16 @@ public boolean isSyncEnabled() {
return preferences.getBoolean(SYNC_ENABLED, false);
}

public void setSyncWifi(boolean isWifi) {
preferences.edit()
.putBoolean(SYNC_WIFI, isWifi)
.apply();
}

public boolean isWifiSync() {
return preferences.getBoolean(SYNC_WIFI, false);
}

public void setSyncTime(int hour, int minutes) {
preferences.edit()
.putString(SYNC_TIME, String.format("%02d", hour) + ":" + String.format("%02d", minutes))
Expand Down Expand Up @@ -112,6 +122,7 @@ public int getSyncCount() {
public static void setSyncAlarm(Context context) {
Intent intent = new Intent(context, DownloadService.class);
intent.setAction(DownloadService.START_SYNC);
intent.putExtra(DownloadService.USER_SYNC, false);
PendingIntent pendingIntent = PendingIntent.getService(context, SYNC_ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
Expand Down
Loading

0 comments on commit 89cb536

Please sign in to comment.