New default avatar photo. Support relative url for profile picture. Refactor download profile picture as async task

This commit is contained in:
Gabriel Calero 2018-04-24 16:43:48 -03:00
parent 55a946c6c7
commit 11d2321467
5 changed files with 120 additions and 31 deletions

View file

@ -1,11 +1,16 @@
package io.highfidelity.hifiinterface;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Created by Gabriel Calero & Cristian Duarte on 4/13/18.
*/
public class HifiUtils {
public static final String METAVERSE_BASE_URL = "https://metaverse.highfidelity.com";
private static HifiUtils instance;
private HifiUtils() {
@ -18,6 +23,26 @@ public class HifiUtils {
return instance;
}
public String absoluteHifiAssetUrl(String urlString) {
return absoluteHifiAssetUrl(urlString, METAVERSE_BASE_URL);
}
public String absoluteHifiAssetUrl(String urlString, String baseUrl) {
urlString = urlString.trim();
if (!urlString.isEmpty()) {
URI uri;
try {
uri = new URI(urlString);
} catch (URISyntaxException e) {
return urlString;
}
if (uri.getScheme() == null || uri.getScheme().isEmpty()) {
urlString = baseUrl + urlString;
}
}
return urlString;
}
public native String getCurrentAddress();
}

View file

@ -35,13 +35,14 @@ import io.highfidelity.hifiinterface.fragment.GotoFragment;
import io.highfidelity.hifiinterface.fragment.HomeFragment;
import io.highfidelity.hifiinterface.fragment.LoginFragment;
import io.highfidelity.hifiinterface.fragment.PolicyFragment;
import io.highfidelity.hifiinterface.task.DownloadProfileImageTask;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener,
LoginFragment.OnLoginInteractionListener,
HomeFragment.OnHomeInteractionListener,
GotoFragment.OnGotoInteractionListener {
private static final int PROFILE_PICTURE_PLACEHOLDER = R.drawable.ic_person;
private static final int PROFILE_PICTURE_PLACEHOLDER = R.drawable.default_profile_avatar;
private String TAG = "HighFidelity";
public native boolean nativeIsLoggedIn();
@ -234,36 +235,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
* TODO: this should be get from an API (at the moment there is no one for this)
*/
private void updateProfilePicture(String username) {
Picasso.get().load(PROFILE_PICTURE_PLACEHOLDER).into(mProfilePicture);
new Thread(() -> {
try {
URL userPage = new URL("https://highfidelity.com/users/" + username);
BufferedReader in = new BufferedReader(
new InputStreamReader(
userPage.openStream()));
StringBuffer strBuff = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null) {
strBuff.append(inputLine);
}
in.close();
String substr = "img class=\"users-img\" src=\"";
int indexBegin = strBuff.indexOf(substr) + substr.length();
if (indexBegin >= substr.length()) {
int indexEnd = strBuff.indexOf("\"", indexBegin);
runOnUiThread(() -> {
if (indexEnd > 0) {
Picasso.get().load(strBuff.substring(indexBegin, indexEnd)).into(mProfilePicture, new RoundProfilePictureCallback());
}
});
}
} catch (IOException e) {
Log.e(TAG, "Error getting profile picture for username " + username);
}
}).start();
mProfilePicture.setImageResource(PROFILE_PICTURE_PLACEHOLDER);
new DownloadProfileImageTask(url -> { if (url!=null && !url.isEmpty()) {
Picasso.get().load(url).into(mProfilePicture, new RoundProfilePictureCallback());
} } ).execute(username);
}
public void onPrivacyPolicyClicked(View view) {

View file

@ -0,0 +1,71 @@
package io.highfidelity.hifiinterface.task;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import io.highfidelity.hifiinterface.HifiUtils;
/**
* This is a temporary solution until the profile picture URL is
* available in an API
*/
public class DownloadProfileImageTask extends AsyncTask<String, Void, String> {
private static final String BASE_PROFILE_URL = "https://highfidelity.com";
private static final String TAG = "Interface";
private final DownloadProfileImageResultProcessor mResultProcessor;
public interface DownloadProfileImageResultProcessor {
void onResultAvailable(String url);
}
public DownloadProfileImageTask(DownloadProfileImageResultProcessor resultProcessor) {
mResultProcessor = resultProcessor;
}
@Override
protected String doInBackground(String... usernames) {
URL userPage = null;
for (String username: usernames) {
try {
userPage = new URL(BASE_PROFILE_URL + "/users/" + username);
BufferedReader in = new BufferedReader(
new InputStreamReader(
userPage.openStream()));
StringBuffer strBuff = new StringBuffer();
String inputLine;
while ((inputLine = in.readLine()) != null) {
strBuff.append(inputLine);
}
in.close();
String substr = "img class=\"users-img\" src=\"";
int indexBegin = strBuff.indexOf(substr) + substr.length();
if (indexBegin >= substr.length()) {
int indexEnd = strBuff.indexOf("\"", indexBegin);
if (indexEnd > 0) {
String url = strBuff.substring(indexBegin, indexEnd);
return HifiUtils.getInstance().absoluteHifiAssetUrl(url, BASE_PROFILE_URL);
}
}
} catch (IOException e) {
Log.e(TAG, "Error getting profile picture for username " + username);
}
}
return null;
}
@Override
protected void onPostExecute(String url) {
super.onPostExecute(url);
if (mResultProcessor != null) {
mResultProcessor.onResultAvailable(url);
}
}
}

View file

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<path
android:fillColor="#8E8E8E"
android:pathData="M50.5,3.42969 C76.4962,3.42969,97.5703,24.5038,97.5703,50.5 C97.5703,76.4962,76.4962,97.5703,50.5,97.5703 C24.5038,97.5703,3.42969,76.4962,3.42969,50.5 C3.42969,24.5038,24.5038,3.42969,50.5,3.42969 Z" />
<path
android:fillColor="#333333"
android:pathData="M53.7813,80.0898 C51.5938,80.0898,49.4063,80.0898,47.1992,80.0898 C47.0625,80.0703,46.9258,80.0507,46.789,80.0312 C44.621,79.914,42.4726,79.8164,40.3046,79.6601 C37.2968,79.4257,34.3085,79.0351,31.3788,78.3515 C29.8945,78,28.4297,77.6094,27.1406,76.7695 C26.6328,76.457,26.3008,76.0664,26.3203,75.4023 C26.3789,72.8632,26.5547,70.3632,27.2383,67.9023 C27.707,66.2226,28.0586,64.5039,28.8594,62.9414 C29.4258,61.8281,29.9336,60.6758,30.5391,59.582 C31.0079,58.7422,31.5547,57.9414,32.1016,57.1406 C33.0196,55.7539,34.1328,54.5234,35.3438,53.3711 C36.3399,52.4141,37.4141,51.5547,38.5469,50.7734 C40.0703,49.7382,41.6914,48.9179,43.4297,48.2734 C45.3242,47.5703,47.2969,47.1601,49.3086,47.0625 C54.1523,46.7891,58.5664,48.1172,62.5508,50.832 C63.6836,51.5937,64.7578,52.4531,65.7149,53.4101 C66.8672,54.5429,67.9024,55.8124,68.918,57.0624 C69.5039,57.8046,70.0118,58.6249,70.4805,59.4647 C71.0664,60.5194,71.6524,61.5741,72.0821,62.6678 C72.668,64.1131,73.2149,65.5975,73.6055,67.1209 C73.9766,68.5662,74.1719,70.0506,74.4258,71.5154 C74.543,72.2771,74.6016,73.0584,74.6797,73.8396 C74.6797,74.5232,74.6797,75.2068,74.6797,75.8904 C74.3672,76.2224,74.0938,76.6326,73.7227,76.867 C73.2149,77.199,72.668,77.4529,72.1016,77.6287 C70.6172,78.0584,69.1329,78.4685,67.6094,78.7615 C64.1719,79.4256,60.6758,79.7185,57.1602,79.9138 C56.0273,79.9727,54.9141,80.0313,53.7813,80.0898 Z" />
<path
android:fillColor="#333333"
android:pathData="M51.6523,20.9102 C52.4922,21.0664,53.3516,21.2031,54.1719,21.418 C54.836,21.5938,55.5,21.7891,56.1055,22.0821 C56.9258,22.4923,57.7266,22.9415,58.4688,23.4688 C60.0508,24.5626,61.2813,25.9883,62.1993,27.6876 C64.1329,31.3009,64.1134,34.9728,62.4727,38.6642 C61.7696,40.2462,60.6758,41.6134,59.3282,42.7072 C56.047,45.383,52.297,46.3791,48.1173,45.715 C46.379,45.4416,44.7775,44.8166,43.2736,43.8791 C42.0627,43.1174,41.008,42.1799,40.09,41.0861 C39.4064,40.2658,38.8986,39.3674,38.4494,38.4103 C36.9455,35.2853,37.0041,32.0822,38.1955,28.8986 C38.5471,27.9416,39.0939,27.0236,39.7189,26.2033 C41.9259,23.3322,44.8361,21.6135,48.4103,21.0666 C48.7228,21.0275,49.0158,20.9689,49.3283,20.9104 C50.1094,20.9102,50.8906,20.9102,51.6523,20.9102 Z" />
</vector>

View file

@ -9,6 +9,7 @@
android:id="@+id/profilePicture"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/default_profile_avatar"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="@dimen/activity_horizontal_margin"