mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-02-16 19:50:35 +01:00
Use new getAvatar() api call.
Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
parent
f2ce22ef08
commit
662e99ff5a
@ -117,7 +117,6 @@ import org.moire.ultrasonic.domain.SearchResult;
|
||||
import org.moire.ultrasonic.domain.Share;
|
||||
import org.moire.ultrasonic.domain.UserInfo;
|
||||
import org.moire.ultrasonic.domain.Version;
|
||||
import org.moire.ultrasonic.service.parser.ErrorParser;
|
||||
import org.moire.ultrasonic.service.parser.SubsonicRESTException;
|
||||
import org.moire.ultrasonic.service.ssl.SSLSocketFactory;
|
||||
import org.moire.ultrasonic.service.ssl.TrustSelfSignedStrategy;
|
||||
@ -138,8 +137,6 @@ import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@ -1373,80 +1370,56 @@ public class RESTMusicService implements MusicService
|
||||
checkResponseSuccessful(response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap getAvatar(Context context, String username, int size, boolean saveToFile, boolean highQuality, ProgressListener progressListener) throws Exception
|
||||
{
|
||||
// Return silently if server is too old
|
||||
if (!checkServerVersion(context, "1.8"))
|
||||
return null;
|
||||
@Override
|
||||
public Bitmap getAvatar(final Context context,
|
||||
final String username,
|
||||
final int size,
|
||||
final boolean saveToFile,
|
||||
final boolean highQuality,
|
||||
final ProgressListener progressListener) throws Exception {
|
||||
// Synchronize on the username so that we don't download concurrently for
|
||||
// the same user.
|
||||
if (username == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Synchronize on the username so that we don't download concurrently for
|
||||
// the same user.
|
||||
if (username == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
synchronized (username) {
|
||||
// Use cached file, if existing.
|
||||
Bitmap bitmap = FileUtil.getAvatarBitmap(username, size, highQuality);
|
||||
|
||||
synchronized (username)
|
||||
{
|
||||
// Use cached file, if existing.
|
||||
Bitmap bitmap = FileUtil.getAvatarBitmap(username, size, highQuality);
|
||||
if (bitmap == null) {
|
||||
InputStream in = null;
|
||||
try {
|
||||
updateProgressListener(progressListener, R.string.parser_reading);
|
||||
StreamResponse response = subsonicAPIClient.getAvatar(username);
|
||||
if (response.hasError()) {
|
||||
return null;
|
||||
}
|
||||
in = response.getStream();
|
||||
byte[] bytes = Util.toByteArray(in);
|
||||
|
||||
if (bitmap == null)
|
||||
{
|
||||
String url = Util.getRestUrl(context, "getAvatar");
|
||||
// If we aren't allowing server-side scaling, always save the file to disk because it will be unmodified
|
||||
if (saveToFile) {
|
||||
OutputStream out = null;
|
||||
|
||||
InputStream in = null;
|
||||
try {
|
||||
out = new FileOutputStream(FileUtil.getAvatarFile(username));
|
||||
out.write(bytes);
|
||||
} finally {
|
||||
Util.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
List<String> parameterNames;
|
||||
List<Object> parameterValues;
|
||||
bitmap = FileUtil.getSampledBitmap(bytes, size, highQuality);
|
||||
} finally {
|
||||
Util.close(in);
|
||||
}
|
||||
}
|
||||
|
||||
parameterNames = Collections.singletonList("username");
|
||||
parameterValues = Arrays.<Object>asList(username);
|
||||
|
||||
HttpEntity entity = getEntityForURL(context, url, null, parameterNames, parameterValues, progressListener);
|
||||
in = entity.getContent();
|
||||
|
||||
// If content type is XML, an error occurred. Get it.
|
||||
String contentType = Util.getContentType(entity);
|
||||
if (contentType != null && contentType.startsWith("text/xml"))
|
||||
{
|
||||
new ErrorParser(context).parse(new InputStreamReader(in, Constants.UTF_8));
|
||||
return null; // Never reached.
|
||||
}
|
||||
|
||||
byte[] bytes = Util.toByteArray(in);
|
||||
|
||||
// If we aren't allowing server-side scaling, always save the file to disk because it will be unmodified
|
||||
if (saveToFile)
|
||||
{
|
||||
OutputStream out = null;
|
||||
|
||||
try
|
||||
{
|
||||
out = new FileOutputStream(FileUtil.getAvatarFile(username));
|
||||
out.write(bytes);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.close(out);
|
||||
}
|
||||
}
|
||||
|
||||
bitmap = FileUtil.getSampledBitmap(bytes, size, highQuality);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.close(in);
|
||||
}
|
||||
}
|
||||
|
||||
// Return scaled bitmap
|
||||
return Util.scaleBitmap(bitmap, size);
|
||||
}
|
||||
}
|
||||
// Return scaled bitmap
|
||||
return Util.scaleBitmap(bitmap, size);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateProgressListener(@Nullable final ProgressListener progressListener,
|
||||
@StringRes final int messageId) {
|
||||
|
@ -1,167 +0,0 @@
|
||||
/*
|
||||
This file is part of Subsonic.
|
||||
|
||||
Subsonic is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Subsonic is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2009 (C) Sindre Mehus
|
||||
*/
|
||||
package org.moire.ultrasonic.service.parser;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Xml;
|
||||
|
||||
import org.moire.ultrasonic.R;
|
||||
import org.moire.ultrasonic.domain.Version;
|
||||
import org.moire.ultrasonic.util.ProgressListener;
|
||||
import org.moire.ultrasonic.util.Util;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* @author Sindre Mehus
|
||||
*/
|
||||
public abstract class AbstractParser
|
||||
{
|
||||
|
||||
private final Context context;
|
||||
private XmlPullParser parser;
|
||||
private boolean rootElementFound;
|
||||
|
||||
public AbstractParser(Context context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
protected Context getContext()
|
||||
{
|
||||
return context;
|
||||
}
|
||||
|
||||
protected void handleError() throws Exception
|
||||
{
|
||||
int code = getInteger("code");
|
||||
String message;
|
||||
switch (code)
|
||||
{
|
||||
case 20:
|
||||
message = context.getResources().getString(R.string.parser_upgrade_client);
|
||||
break;
|
||||
case 30:
|
||||
message = context.getResources().getString(R.string.parser_upgrade_server);
|
||||
break;
|
||||
case 40:
|
||||
message = context.getResources().getString(R.string.parser_not_authenticated);
|
||||
break;
|
||||
case 50:
|
||||
message = context.getResources().getString(R.string.parser_not_authorized);
|
||||
break;
|
||||
default:
|
||||
message = get("message");
|
||||
break;
|
||||
}
|
||||
throw new SubsonicRESTException(code, message);
|
||||
}
|
||||
|
||||
protected void updateProgress(ProgressListener progressListener, int messageId)
|
||||
{
|
||||
if (progressListener != null)
|
||||
{
|
||||
progressListener.updateProgress(messageId);
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateProgress(ProgressListener progressListener, String message)
|
||||
{
|
||||
if (progressListener != null)
|
||||
{
|
||||
progressListener.updateProgress(message);
|
||||
}
|
||||
}
|
||||
|
||||
protected String getText()
|
||||
{
|
||||
return parser.getText();
|
||||
}
|
||||
|
||||
protected String get(String name)
|
||||
{
|
||||
return parser.getAttributeValue(null, name);
|
||||
}
|
||||
|
||||
protected boolean getBoolean(String name)
|
||||
{
|
||||
return "true".equals(get(name));
|
||||
}
|
||||
|
||||
protected boolean getValueExists(String name)
|
||||
{
|
||||
String value = get(name);
|
||||
return value != null && !value.isEmpty();
|
||||
}
|
||||
|
||||
protected Integer getInteger(String name)
|
||||
{
|
||||
String s = get(name);
|
||||
return s == null ? null : Integer.valueOf(s);
|
||||
}
|
||||
|
||||
protected Long getLong(String name)
|
||||
{
|
||||
String s = get(name);
|
||||
return s == null ? null : Long.valueOf(s);
|
||||
}
|
||||
|
||||
protected Float getFloat(String name)
|
||||
{
|
||||
String s = get(name);
|
||||
return s == null ? null : Float.valueOf(s);
|
||||
}
|
||||
|
||||
protected void init(Reader reader) throws Exception
|
||||
{
|
||||
parser = Xml.newPullParser();
|
||||
parser.setInput(reader);
|
||||
rootElementFound = false;
|
||||
}
|
||||
|
||||
protected int nextParseEvent() throws Exception
|
||||
{
|
||||
return parser.next();
|
||||
}
|
||||
|
||||
protected String getElementName()
|
||||
{
|
||||
String name = parser.getName();
|
||||
if ("subsonic-response".equals(name))
|
||||
{
|
||||
rootElementFound = true;
|
||||
String version = get("version");
|
||||
if (version != null)
|
||||
{
|
||||
Util.setServerRestVersion(context, new Version(version));
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
protected void validate() throws Exception
|
||||
{
|
||||
if (!rootElementFound)
|
||||
{
|
||||
throw new Exception(context.getResources().getString(R.string.background_task_parse_error));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
/*
|
||||
This file is part of Subsonic.
|
||||
|
||||
Subsonic is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Subsonic is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2009 (C) Sindre Mehus
|
||||
*/
|
||||
package org.moire.ultrasonic.service.parser;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* @author Sindre Mehus
|
||||
*/
|
||||
public class ErrorParser extends AbstractParser
|
||||
{
|
||||
|
||||
public ErrorParser(Context context)
|
||||
{
|
||||
super(context);
|
||||
}
|
||||
|
||||
public void parse(Reader reader) throws Exception
|
||||
{
|
||||
|
||||
init(reader);
|
||||
|
||||
int eventType;
|
||||
do
|
||||
{
|
||||
eventType = nextParseEvent();
|
||||
if (eventType == XmlPullParser.START_TAG && "error".equals(getElementName()))
|
||||
{
|
||||
handleError();
|
||||
}
|
||||
} while (eventType != XmlPullParser.END_DOCUMENT);
|
||||
|
||||
validate();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user