diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 540c9db4..5c8c756a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,6 +10,7 @@ android:allowBackup="true" android:fullBackupContent="true" android:icon="@drawable/ic_launcher" + android:name=".App" android:label="@string/app_name" android:theme="@style/AppTheme" > 0 && progress <= 60) { Helpers.getNotificationCount(wv); + Helpers.getUserProfile(wv); } if (progress > 60) { @@ -494,7 +500,7 @@ public class MainActivity extends AppCompatActivity case R.id.action_exit: { moveTaskToBack(true); } - break; + return true; case R.id.action_share: { final CharSequence[] options = {getString(R.string.share_link), getString(R.string.share_screenshot), getString(R.string.take_screenshot)}; @@ -630,7 +636,7 @@ public class MainActivity extends AppCompatActivity } }).show(); } - break; + return true; } return super.onOptionsItemSelected(item); } @@ -722,15 +728,22 @@ public class MainActivity extends AppCompatActivity @JavascriptInterface public void setProfileId(final String webMessage) { - if(profileId.equals("") || !profileId.equals(webMessage)) { + if (profileId.equals("") || !profileId.equals(webMessage)) { profileId = webMessage; appSettings.setProfileId(profileId); } } + @JavascriptInterface + public void setUserProfile(final String webMessage) throws JSONException { + JSONObject d = new JSONObject(webMessage); - @JavascriptInterface + int id = d.getInt("id"); + System.out.print(id); + } + + @JavascriptInterface public void setConversationCount(final String webMessage) { myHandler.post(new Runnable() { @Override diff --git a/app/src/main/java/de/baumann/diaspora/WebUserProfile.java b/app/src/main/java/de/baumann/diaspora/WebUserProfile.java new file mode 100644 index 00000000..b41d2ce0 --- /dev/null +++ b/app/src/main/java/de/baumann/diaspora/WebUserProfile.java @@ -0,0 +1,138 @@ +package de.baumann.diaspora; + +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.concurrent.TimeUnit; + +/** + * Created by de-live-gdev on 24.03.16. Part of Diaspora WebApp. + */ +public class WebUserProfile { + private final int MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF = 5000; + JSONObject json; + long lastLoaded; + boolean isWebUserProfileLoaded; + + public WebUserProfile(){ + } + + public boolean isRefreshNeeded(){ + return (System.currentTimeMillis() - lastLoaded) >= MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF; + } + + public boolean isWebUserProfileLoaded() { + return isWebUserProfileLoaded; + } + + public boolean loadFromJson(String json) { + try { + this.json = new JSONObject(json); + lastLoaded = System.currentTimeMillis(); + isWebUserProfileLoaded = true; + } catch (JSONException e) { + Log.d(App.APP_LOG_TAG, e.getMessage()); + isWebUserProfileLoaded = false; + } + return isWebUserProfileLoaded; + } + + /** + * Get the Avatar URL's + * @return Avatar URL's + * [0] small + * [1] medium + * [2] large + */ + public String[] getAvatarUrls(){ + try { + String[] avatars = new String[3]; + JSONObject o = json.getJSONObject("avatar"); + avatars[0] = o.getString("small"); + avatars[1] = o.getString("medium"); + avatars[2] = o.getString("large"); + return avatars; + } catch (JSONException e) { + return null; + } + } + + public int getId(){ + try { + return json.getInt("id"); + } catch (JSONException e) { + return 0; + } + } + + /** + * Get the users profile address id + * @return guid + */ + public int getGuid(){ + try { + return json.getInt("guid"); + } catch (JSONException e) { + return 0; + } + } + + public String getName(){ + try { + return json.getString("guid"); + } catch (JSONException e) { + return null; + } + } + + public String getDiasporaAddress(){ + try { + return json.getString("diaspora_id"); + } catch (JSONException e) { + return null; + } + } + + public int getNotificationCount(){ + try { + return json.getInt("notifications_count"); + } catch (JSONException e) { + return 0; + } + } + + public int getUnreadMessagesCount(){ + try { + return json.getInt("unread_messages_count"); + } catch (JSONException e) { + return 0; + } + } + + public int getFollowingCount(){ + try { + return json.getInt("following_count"); + } catch (JSONException e) { + return 0; + } + } + + + /* + * Not implemented / not needed yet: + * boolean "admin" + * boolean "moderator" + * array "aspects" + * int "id" + * string "name" + * boolean "selected" + * + * array "services" + * ? ? + * array "configured_services" + * ? ? + * + */ +} diff --git a/app/src/main/java/de/baumann/diaspora/utils/Helpers.java b/app/src/main/java/de/baumann/diaspora/utils/Helpers.java index 7a9a8e2b..d35bf424 100644 --- a/app/src/main/java/de/baumann/diaspora/utils/Helpers.java +++ b/app/src/main/java/de/baumann/diaspora/utils/Helpers.java @@ -70,4 +70,13 @@ public class Helpers { " } " + "})();"); } + + public static void getUserProfile(final WebView wv) { + wv.loadUrl("javascript: ( function() {" + + " if (typeof gon !== 'undefined' && typeof gon.user !== 'undefined') {" + + " var userProfile = JSON.stringify(gon.user);" + + " AndroidBridge.setUserProfile(userProfile.toString());" + + " } " + + "})();"); + } }