diff --git a/app/build.gradle b/app/build.gradle index 0af025f3..73730b47 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -26,4 +26,5 @@ dependencies { compile 'com.android.support:appcompat-v7:23.2.1' compile 'com.android.support:design:23.2.1' compile 'com.getbase:floatingactionbutton:1.9.1' + compile 'com.jakewharton:butterknife:7.0.1' } diff --git a/app/src/main/java/de/baumann/diaspora/App.java b/app/src/main/java/de/baumann/diaspora/App.java index a0524fad..93e387ff 100644 --- a/app/src/main/java/de/baumann/diaspora/App.java +++ b/app/src/main/java/de/baumann/diaspora/App.java @@ -2,20 +2,30 @@ package de.baumann.diaspora; import android.app.Application; +import java.io.File; + +import de.baumann.diaspora.utils.AvatarImageLoader; + /** * Created by gregor on 24.03.16. */ public class App extends Application { private AppSettings appSettings; + private AvatarImageLoader avatarImageLoader; public static final String APP_LOG_TAG = "DIASPORA_"; @Override public void onCreate() { super.onCreate(); appSettings = new AppSettings(getApplicationContext()); + avatarImageLoader = new AvatarImageLoader(getApplicationContext()); } public AppSettings getSettings() { return appSettings; } + + public AvatarImageLoader getAvatarImageLoader() { + return avatarImageLoader; + } } diff --git a/app/src/main/java/de/baumann/diaspora/AppSettings.java b/app/src/main/java/de/baumann/diaspora/AppSettings.java index 81c256e9..39063b31 100644 --- a/app/src/main/java/de/baumann/diaspora/AppSettings.java +++ b/app/src/main/java/de/baumann/diaspora/AppSettings.java @@ -2,17 +2,17 @@ package de.baumann.diaspora; import android.content.Context; import android.content.SharedPreferences; -import android.preference.PreferenceManager; /** * Created by de-live-gdev on 20.03.16. Part of Diaspora WebApp. */ class AppSettings { private final SharedPreferences pref; + private final Context context; public AppSettings(Context context) { - Context context1 = context.getApplicationContext(); - pref = context1.getSharedPreferences("app", Context.MODE_PRIVATE); + this.context = context.getApplicationContext(); + pref = this.context.getSharedPreferences("app", Context.MODE_PRIVATE); } private void setString(String key, String value) { @@ -28,20 +28,24 @@ class AppSettings { /* // Preferences */ - private static final String PREF_PROFILE_ID = "profileID"; + private static final String PREF_WEBUSERPROFILE_ID = "webUserProfile_guid"; private static final String PREF_IS_LOAD_IMAGES = "loadImages"; private static final String PREF_MINIMUM_FONT_SIZE = "minimumFontSize"; + private static final String PREF_AVATAR_URL = "webUserProfile_avatar"; + private static final String PREF_WEBUSERPROFILE_NAME = "webUserProfile_name"; + private static final String PREF_PODDOMAIN = "podDomain"; + /* // Setters & Getters */ public String getProfileId() { - return pref.getString(PREF_PROFILE_ID, ""); + return pref.getString(PREF_WEBUSERPROFILE_ID, ""); } public void setProfileId(String profileId) { - setString(PREF_PROFILE_ID, profileId); + setString(PREF_WEBUSERPROFILE_ID, profileId); } @@ -61,4 +65,30 @@ class AppSettings { public void setMinimumFontSize(int minimumFontSize) { setInt(PREF_MINIMUM_FONT_SIZE, minimumFontSize); } + + public String getAvatarUrl() { + return pref.getString(PREF_AVATAR_URL, ""); + } + + public void setAvatarUrl(String avatarUrl) { + setString(PREF_AVATAR_URL, avatarUrl); + } + + public String getName(){ + return pref.getString(PREF_WEBUSERPROFILE_NAME, ""); + } + + public void setName(String name){ + setString(PREF_WEBUSERPROFILE_NAME, name); + } + + public String getPodDomain(){ + return pref.getString(PREF_PODDOMAIN, ""); + } + + public void setPodDomain(String podDomain){ + setString(PREF_PODDOMAIN, podDomain); + } + + } diff --git a/app/src/main/java/de/baumann/diaspora/ImageDownloadTask.java b/app/src/main/java/de/baumann/diaspora/ImageDownloadTask.java new file mode 100644 index 00000000..289e3282 --- /dev/null +++ b/app/src/main/java/de/baumann/diaspora/ImageDownloadTask.java @@ -0,0 +1,65 @@ +package de.baumann.diaspora; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.AsyncTask; +import android.support.annotation.Nullable; +import android.util.Log; +import android.widget.ImageView; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * Created by Gregor Santner (de-live-gdev) on 24.03.16. + */ +public class ImageDownloadTask extends AsyncTask { + ImageView imageView; + String savePath; + + /** + * Download image from URL + * + * @param imageView ImageView to set image to (null = don't set) + * @param savePath Save image to file (null = don't save) + */ + public ImageDownloadTask(@Nullable ImageView imageView, @Nullable String savePath) { + this.imageView = imageView; + this.savePath = savePath; + } + + protected Bitmap doInBackground(String... urls) { + String url = urls[0]; + Bitmap bitmap = null; + FileOutputStream out = null; + try { + InputStream in = new java.net.URL(url).openStream(); + bitmap = BitmapFactory.decodeStream(in); + + // Save to file if not null + if (savePath != null) { + out = new FileOutputStream(savePath); + bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); + } + + } catch (Exception e) { + Log.e(App.APP_LOG_TAG, e.getMessage()); + } finally { + try { + if (out != null) { + out.close(); + } + } catch (IOException e) { + } + } + return bitmap; + } + + protected void onPostExecute(Bitmap result) { + // Display on imageview if not null + if (imageView != null) { + imageView.setImageBitmap(result); + } + } +} diff --git a/app/src/main/java/de/baumann/diaspora/MainActivity.java b/app/src/main/java/de/baumann/diaspora/MainActivity.java index 4f34eb16..f2f24c03 100644 --- a/app/src/main/java/de/baumann/diaspora/MainActivity.java +++ b/app/src/main/java/de/baumann/diaspora/MainActivity.java @@ -29,7 +29,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; -import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -63,13 +62,15 @@ import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.EditText; +import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; +import com.getbase.floatingactionbutton.FloatingActionsMenu; + import org.json.JSONException; -import org.json.JSONObject; import java.io.File; import java.io.FileOutputStream; @@ -77,10 +78,12 @@ import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; +import butterknife.Bind; +import butterknife.ButterKnife; import de.baumann.diaspora.utils.Helpers; public class MainActivity extends AppCompatActivity - implements NavigationView.OnNavigationItemSelectedListener { + implements NavigationView.OnNavigationItemSelectedListener, WebUserProfileChangedListener { static final int INPUT_FILE_REQUEST_CODE = 1; @@ -88,20 +91,37 @@ public class MainActivity extends AppCompatActivity private static final String URL_MESSAGE = "URL_MESSAGE"; private App app; - private final Handler myHandler = new Handler(); - private WebView webView; private String podDomain; private Menu menu; private int notificationCount = 0; private int conversationCount = 0; - private String profileId = ""; private ValueCallback mFilePathCallback; private String mCameraPhotoPath; - private com.getbase.floatingactionbutton.FloatingActionsMenu fab; - private ProgressBar progressBar; - private WebSettings wSettings; + private WebSettings webSettings; private AppSettings appSettings; - private SwipeRefreshLayout swipeView; + private WebUserProfile webUserProfile; + private final Handler uiHandler = new Handler(); + + @Bind(R.id.swipe) + SwipeRefreshLayout swipeRefreshLayout; + + @Bind(R.id.progressBar) + ProgressBar progressBar; + + @Bind(R.id.toolbar) + Toolbar toolbar; + + @Bind(R.id.webView) + WebView webView; + + @Bind(R.id.fab_menubutton) + FloatingActionsMenu fab; + + // NavHeader cannot be bound by Butterknife + private TextView navheaderTitle; + private TextView navheaderDescription; + private ImageView navheaderImage; + @SuppressLint("SetJavaScriptEnabled") @Override @@ -111,11 +131,16 @@ public class MainActivity extends AppCompatActivity if (android.os.Build.VERSION.SDK_INT >= 21) WebView.enableSlowWholeDocumentDraw(); + // Bind UI setContentView(R.layout.activity_main); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - app = (App) getApplication(); - setSupportActionBar(toolbar); + ButterKnife.bind(this); + app = (App) getApplication(); + appSettings = app.getSettings(); + webUserProfile = new WebUserProfile(app,uiHandler,this); + + // Setup toolbar + setSupportActionBar(toolbar); toolbar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { @@ -123,54 +148,36 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/stream"); setTitle(R.string.jb_stream); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } }); // Load app settings - appSettings = app.getSettings(); - profileId = appSettings.getProfileId(); - - DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); - ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( - this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); - drawer.addDrawerListener(toggle); - toggle.syncState(); - - NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); - navigationView.setNavigationItemSelectedListener(this); + setupNavigationSlider(); progressBar = (ProgressBar) findViewById(R.id.progressBar); + podDomain = appSettings.getPodDomain(); - SharedPreferences config = getSharedPreferences("PodSettings", MODE_PRIVATE); - podDomain = config.getString("podDomain", null); - - fab = (com.getbase.floatingactionbutton.FloatingActionsMenu) findViewById(R.id.multiple_actions); - fab.setVisibility(View.GONE); - - swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe); - swipeView.setColorSchemeResources(R.color.colorPrimary, + swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.fab_big); - webView = (WebView) findViewById(R.id.webView); webView.addJavascriptInterface(new JavaScriptInterface(), "AndroidBridge"); - if (savedInstanceState != null) { webView.restoreState(savedInstanceState); } - wSettings = webView.getSettings(); - wSettings.setJavaScriptEnabled(true); - wSettings.setUseWideViewPort(true); - wSettings.setLoadWithOverviewMode(true); - wSettings.setDomStorageEnabled(true); - wSettings.setMinimumFontSize(appSettings.getMinimumFontSize()); - wSettings.setLoadsImagesAutomatically(appSettings.isLoadImages()); + webSettings = webView.getSettings(); + webSettings.setJavaScriptEnabled(true); + webSettings.setUseWideViewPort(true); + webSettings.setLoadWithOverviewMode(true); + webSettings.setDomStorageEnabled(true); + webSettings.setMinimumFontSize(appSettings.getMinimumFontSize()); + webSettings.setLoadsImagesAutomatically(appSettings.isLoadImages()); if (android.os.Build.VERSION.SDK_INT >= 21) - wSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); + webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); /* * WebViewClient @@ -186,18 +193,18 @@ public class MainActivity extends AppCompatActivity } public void onPageFinished(WebView view, String url) { - swipeView.setRefreshing(false); + swipeRefreshLayout.setRefreshing(false); } }); - swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { if (Helpers.isOnline(MainActivity.this)) { webView.reload(); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); - swipeView.setRefreshing(false); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + swipeRefreshLayout.setRefreshing(false); } } }); @@ -217,7 +224,6 @@ public class MainActivity extends AppCompatActivity if (progress > 60) { Helpers.hideTopBar(wv); - Helpers.getProfileId(wv); fab.setVisibility(View.VISIBLE); } @@ -244,7 +250,7 @@ public class MainActivity extends AppCompatActivity takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath); } catch (IOException ex) { // Error occurred while creating the File - Snackbar.make(swipeView, R.string.image, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.image, Snackbar.LENGTH_LONG).show(); return false; } @@ -286,12 +292,42 @@ public class MainActivity extends AppCompatActivity webView.loadData("", "text/html", null); webView.loadUrl("https://" + podDomain); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } } + private void setupNavigationSlider(){ + DrawerLayout drawer = ButterKnife.findById(this, R.id.drawer_layout); + ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( + this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); + drawer.addDrawerListener(toggle); + toggle.syncState(); + + NavigationView navigationView = ButterKnife.findById(this, R.id.nav_view); + navigationView.setNavigationItemSelectedListener(this); + + View navHeader = navigationView.getHeaderView(0); + navheaderTitle = ButterKnife.findById(navHeader,R.id.navheader_title); + navheaderDescription = ButterKnife.findById(navHeader,R.id.navheader_description); + navheaderImage = ButterKnife.findById(navHeader,R.id.navheader_user_image); + + if(!appSettings.getName().equals("")) { + navheaderTitle.setText(appSettings.getName()); + } + if(!appSettings.getPodDomain().equals("")){ + navheaderDescription.setText(appSettings.getPodDomain()); + } + if(!appSettings.getAvatarUrl().equals("")){ + // Try to load image + if(!app.getAvatarImageLoader().loadToImageView(navheaderImage)){ + // If not yet loaded, start download + app.getAvatarImageLoader().startImageDownload(navheaderImage, appSettings.getAvatarUrl()); + } + } + } + /* * Fab button events */ @@ -302,7 +338,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/status_messages/new"); setTitle(R.string.fab1_title); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } @@ -321,7 +357,7 @@ public class MainActivity extends AppCompatActivity // this validate the input data for tagfind if (cleanTag == null || cleanTag.equals("")) { dialog.cancel(); // if user don�t have added a tag - Snackbar.make(swipeView, R.string.search_alert_bypeople_validate_needsomedata, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.search_alert_bypeople_validate_needsomedata, Snackbar.LENGTH_LONG).show(); } else { // User have added a search tag webView.loadUrl("https://" + podDomain + "/people.mobile?q=" + cleanTag); setTitle(R.string.fab2_title_person); @@ -336,7 +372,7 @@ public class MainActivity extends AppCompatActivity // this validate the input data for tagfind if (cleanTag == null || cleanTag.equals("")) { dialog.cancel(); // if user hasn't added a tag - Snackbar.make(swipeView, R.string.search_alert_bytags_validate_needsomedata, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.search_alert_bytags_validate_needsomedata, Snackbar.LENGTH_LONG).show(); } else { // User have added a search tag webView.loadUrl("https://" + podDomain + "/tags/" + cleanTag); setTitle(R.string.fab2_title_tag); @@ -345,7 +381,7 @@ public class MainActivity extends AppCompatActivity }); dialog.show(); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } @@ -418,7 +454,7 @@ public class MainActivity extends AppCompatActivity setTitle(R.string.app_name); } else { Snackbar snackbar = Snackbar - .make(swipeView, R.string.confirm_exit, Snackbar.LENGTH_LONG) + .make(swipeRefreshLayout, R.string.confirm_exit, Snackbar.LENGTH_LONG) .setAction(R.string.yes, new View.OnClickListener() { @Override public void onClick(View view) { @@ -481,7 +517,7 @@ public class MainActivity extends AppCompatActivity setTitle(R.string.jb_notifications); return true; } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); return false; } } @@ -492,7 +528,7 @@ public class MainActivity extends AppCompatActivity setTitle(R.string.jb_conversations); return true; } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); return false; } } @@ -510,7 +546,7 @@ public class MainActivity extends AppCompatActivity public void onClick(DialogInterface dialog, int item) { if (options[item].equals(getString(R.string.share_link))) { Intent sharingIntent = new Intent(Intent.ACTION_SEND); - sharingIntent.setType("image/png"); + sharingIntent.setType("text/plain"); sharingIntent.putExtra(Intent.EXTRA_SUBJECT, webView.getTitle()); sharingIntent.putExtra(Intent.EXTRA_TEXT, webView.getUrl()); startActivity(Intent.createChooser(sharingIntent, "Share using")); @@ -539,7 +575,7 @@ public class MainActivity extends AppCompatActivity return; } } - Snackbar.make(swipeView, R.string.toast_screenshot, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.toast_screenshot, Snackbar.LENGTH_LONG).show(); File directory = new File(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/"); if (!directory.exists()) { directory.mkdirs(); @@ -602,7 +638,7 @@ public class MainActivity extends AppCompatActivity return; } } - Snackbar.make(swipeView, R.string.toast_screenshot, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.toast_screenshot, Snackbar.LENGTH_LONG).show(); File directory = new File(Environment.getExternalStorageDirectory() + "/Pictures/Diaspora/"); if (!directory.exists()) { directory.mkdirs(); @@ -677,22 +713,44 @@ public class MainActivity extends AppCompatActivity appSettings.setMinimumFontSize(20); } - wSettings.setMinimumFontSize(appSettings.getMinimumFontSize()); + webSettings.setMinimumFontSize(appSettings.getMinimumFontSize()); if (Helpers.isOnline(MainActivity.this)) { webView.loadUrl(webView.getUrl()); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } dialog.cancel(); } }).show(); } + @Override + public void onUserProfileNameChanged(String name) { + navheaderTitle.setText(name); + } + + @Override + public void onUserProfileAvatarChanged(String avatarUrl) { + app.getAvatarImageLoader().startImageDownload(navheaderImage, avatarUrl); + } + + // TODO: Move from Javascript interface + @Override + public void onNotificationCountChanged(int notificationCount) { + + } + + // TODO: Move from Javascript interface + @Override + public void onUnreadMessageCountChanged(int unreadMessageCount) { + + } + private class JavaScriptInterface { @JavascriptInterface public void setNotificationCount(final String webMessage) { - myHandler.post(new Runnable() { + uiHandler.post(new Runnable() { @Override public void run() { notificationCount = Integer.valueOf(webMessage); @@ -703,7 +761,7 @@ public class MainActivity extends AppCompatActivity if (notificationCount > 0) { item.setIcon(R.drawable.ic_bell_ring_white_24dp); Snackbar snackbar = Snackbar - .make(swipeView, R.string.new_notifications, Snackbar.LENGTH_LONG) + .make(swipeRefreshLayout, R.string.new_notifications, Snackbar.LENGTH_LONG) .setAction(R.string.yes, new View.OnClickListener() { @Override public void onClick(View view) { @@ -711,7 +769,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/notifications"); setTitle(R.string.jb_notifications); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } }); @@ -726,26 +784,16 @@ public class MainActivity extends AppCompatActivity }); } - @JavascriptInterface - public void setProfileId(final String 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); - - int id = d.getInt("id"); - System.out.print(id); + if (webUserProfile.isRefreshNeeded()){ + webUserProfile.parseJson(webMessage); + } } @JavascriptInterface public void setConversationCount(final String webMessage) { - myHandler.post(new Runnable() { + uiHandler.post(new Runnable() { @Override public void run() { conversationCount = Integer.valueOf(webMessage); @@ -756,7 +804,7 @@ public class MainActivity extends AppCompatActivity if (conversationCount > 0) { item.setIcon(R.drawable.ic_message_text_white_24dp); Snackbar snackbar = Snackbar - .make(swipeView, R.string.new_conversations, Snackbar.LENGTH_LONG) + .make(swipeRefreshLayout, R.string.new_conversations, Snackbar.LENGTH_LONG) .setAction(R.string.yes, new View.OnClickListener() { @Override public void onClick(View view) { @@ -764,7 +812,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/conversations"); setTitle(R.string.jb_notifications); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } }); @@ -790,17 +838,17 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/stream"); setTitle(R.string.jb_stream); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; case R.id.nav_profile: { if (Helpers.isOnline(MainActivity.this)) { - webView.loadUrl("https://" + podDomain + "/people/" + profileId); + webView.loadUrl("https://" + podDomain + "/people/" + appSettings.getProfileId()); setTitle(R.string.jb_profile); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -811,7 +859,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/followed_tags"); setTitle(R.string.jb_followed_tags); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -821,7 +869,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/aspects"); setTitle(R.string.jb_aspects); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -831,7 +879,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/activity"); setTitle(R.string.jb_activities); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -841,7 +889,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/liked"); setTitle(R.string.jb_liked); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -851,7 +899,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/commented"); setTitle(R.string.jb_commented); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -861,7 +909,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/mentions"); setTitle(R.string.jb_mentions); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -871,7 +919,7 @@ public class MainActivity extends AppCompatActivity webView.loadUrl("https://" + podDomain + "/public"); setTitle(R.string.jb_public); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -888,13 +936,13 @@ public class MainActivity extends AppCompatActivity if (options[item].equals(getString(R.string.settings_view))) webView.loadUrl("https://" + podDomain + "/mobile/toggle"); if (options[item].equals(getString(R.string.settings_image))) - wSettings.setLoadsImagesAutomatically(!appSettings.isLoadImages()); + webSettings.setLoadsImagesAutomatically(!appSettings.isLoadImages()); appSettings.setLoadImages(!appSettings.isLoadImages()); webView.loadUrl(webView.getUrl()); } }).show(); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; @@ -937,7 +985,7 @@ public class MainActivity extends AppCompatActivity } }).show(); } else { - Snackbar.make(swipeView, R.string.no_internet, Snackbar.LENGTH_LONG).show(); + Snackbar.make(swipeRefreshLayout, R.string.no_internet, Snackbar.LENGTH_LONG).show(); } } break; diff --git a/app/src/main/java/de/baumann/diaspora/PodsActivity.java b/app/src/main/java/de/baumann/diaspora/PodsActivity.java index 4918378e..da2d3600 100644 --- a/app/src/main/java/de/baumann/diaspora/PodsActivity.java +++ b/app/src/main/java/de/baumann/diaspora/PodsActivity.java @@ -58,11 +58,13 @@ public class PodsActivity extends AppCompatActivity { private EditText filter; private ListView lv; private ProgressDialog progressDialog; + private App app; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pods); + app = (App) getApplication(); filter = (EditText) findViewById(R.id.edtFilter); lv = (ListView) findViewById(R.id.lstPods); @@ -168,10 +170,7 @@ public class PodsActivity extends AppCompatActivity { new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - SharedPreferences sp = getSharedPreferences("PodSettings", MODE_PRIVATE); - SharedPreferences.Editor editor = sp.edit(); - editor.putString("podDomain", podDomain); - editor.apply(); + app.getSettings().setPodDomain(podDomain); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { try { diff --git a/app/src/main/java/de/baumann/diaspora/ShareActivity.java b/app/src/main/java/de/baumann/diaspora/ShareActivity.java index c316cdd9..56f0acde 100644 --- a/app/src/main/java/de/baumann/diaspora/ShareActivity.java +++ b/app/src/main/java/de/baumann/diaspora/ShareActivity.java @@ -88,10 +88,9 @@ public class ShareActivity extends MainActivity { }); - SharedPreferences config = getSharedPreferences("PodSettings", MODE_PRIVATE); - podDomain = config.getString("podDomain", null); + podDomain = ((App)getApplication()).getSettings().getPodDomain(); - fab = (com.getbase.floatingactionbutton.FloatingActionsMenu) findViewById(R.id.multiple_actions); + fab = (com.getbase.floatingactionbutton.FloatingActionsMenu) findViewById(R.id.fab_menubutton); fab.setVisibility(View.GONE); webView = (WebView)findViewById(R.id.webView); diff --git a/app/src/main/java/de/baumann/diaspora/ShareActivity2.java b/app/src/main/java/de/baumann/diaspora/ShareActivity2.java index c8c33fd2..5eeafb23 100644 --- a/app/src/main/java/de/baumann/diaspora/ShareActivity2.java +++ b/app/src/main/java/de/baumann/diaspora/ShareActivity2.java @@ -88,10 +88,9 @@ public class ShareActivity2 extends MainActivity { }); - SharedPreferences config = getSharedPreferences("PodSettings", MODE_PRIVATE); - podDomain = config.getString("podDomain", null); + podDomain = ((App)getApplication()).getSettings().getPodDomain(); - fab = (com.getbase.floatingactionbutton.FloatingActionsMenu) findViewById(R.id.multiple_actions); + fab = (com.getbase.floatingactionbutton.FloatingActionsMenu) findViewById(R.id.fab_expand_menu_button); fab.setVisibility(View.GONE); webView = (WebView)findViewById(R.id.webView); diff --git a/app/src/main/java/de/baumann/diaspora/SplashActivity.java b/app/src/main/java/de/baumann/diaspora/SplashActivity.java index 1f9fc3ae..b1a4648f 100644 --- a/app/src/main/java/de/baumann/diaspora/SplashActivity.java +++ b/app/src/main/java/de/baumann/diaspora/SplashActivity.java @@ -32,6 +32,7 @@ import java.util.TimerTask; public class SplashActivity extends AppCompatActivity { + private App app; @Override protected void onCreate(Bundle savedInstanceState) { @@ -39,6 +40,7 @@ public class SplashActivity extends AppCompatActivity { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_splash); + app = (App)getApplication(); ImageView imgSplash = (ImageView) findViewById(R.id.imgSplash); @@ -47,14 +49,13 @@ public class SplashActivity extends AppCompatActivity { imgSplash.setImageResource(images.getResourceId(choice, R.drawable.splashscreen1)); images.recycle(); - final SharedPreferences config = getSharedPreferences("PodSettings", MODE_PRIVATE); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Intent i; - if (config.getString("podDomain", null) != null) { + if (!app.getSettings().getPodDomain().equals("")) { i = new Intent(SplashActivity.this, MainActivity.class); } else { i = new Intent(SplashActivity.this, PodsActivity.class); diff --git a/app/src/main/java/de/baumann/diaspora/WebUserProfile.java b/app/src/main/java/de/baumann/diaspora/WebUserProfile.java index b41d2ce0..5ba29674 100644 --- a/app/src/main/java/de/baumann/diaspora/WebUserProfile.java +++ b/app/src/main/java/de/baumann/diaspora/WebUserProfile.java @@ -1,25 +1,45 @@ package de.baumann.diaspora; +import android.os.Handler; import android.util.Log; import org.json.JSONException; import org.json.JSONObject; -import java.util.concurrent.TimeUnit; +import java.io.File; /** * 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; + private static final int MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF = 5000; - public WebUserProfile(){ + private Handler uiHandler; + private WebUserProfileChangedListener listener; + private App app; + private AppSettings appSettings; + private JSONObject json; + private long lastLoaded; + private boolean isWebUserProfileLoaded; + + private String avatarUrl; + private String guid; + private String name; + private int notificationCount; + private int unreadMessagesCount; + + public WebUserProfile(App app, Handler uiHandler, WebUserProfileChangedListener listener) { + this.listener = listener; + this.uiHandler = uiHandler; + this.app = app; + appSettings = app.getSettings(); + + avatarUrl = appSettings.getAvatarUrl(); + guid = appSettings.getProfileId(); + name = appSettings.getName(); } - public boolean isRefreshNeeded(){ + public boolean isRefreshNeeded() { return (System.currentTimeMillis() - lastLoaded) >= MINIMUM_WEBUSERPROFILE_LOAD_TIMEDIFF; } @@ -27,102 +47,105 @@ public class WebUserProfile { return isWebUserProfileLoaded; } - public boolean loadFromJson(String json) { + public boolean parseJson(String jsonStr) { try { - this.json = new JSONObject(json); + this.json = new JSONObject(jsonStr); lastLoaded = System.currentTimeMillis(); + + String str; + int integer; + + // Avatar + if (json.has("avatar")) { + JSONObject avatarJson = json.getJSONObject("avatar"); + if (avatarJson.has("medium") && !((str = avatarJson.getString("medium")).equals(avatarUrl))) { + app.getAvatarImageLoader().clearAvatarImage(); + avatarUrl = str; + appSettings.setAvatarUrl(str); + uiHandler.post(new Runnable() { + public void run() { + listener.onUserProfileAvatarChanged(avatarUrl); + } + }); + } + } + + // GUID (User id) + if (json.has("guid") && !((str = json.getString("guid")).equals(guid))) { + guid = str; + appSettings.setProfileId(guid); + } + + // Name + if (json.has("name") && !((str = json.getString("name")).equals(name))) { + name = str; + appSettings.setName(name); + uiHandler.post(new Runnable() { + public void run() { + listener.onUserProfileNameChanged(name); + } + }); + } + + // Unread message count + if (json.has("notifications_count") && (integer = json.getInt("notifications_count")) != notificationCount) { + notificationCount = integer; + uiHandler.post(new Runnable() { + public void run() { + listener.onNotificationCountChanged(notificationCount); + } + }); + } + + // Unread message count + if (json.has("unread_messages_count") && (integer = json.getInt("unread_messages_count")) != unreadMessagesCount) { + unreadMessagesCount = integer; + uiHandler.post(new Runnable() { + public void run() { + listener.onUnreadMessageCountChanged(unreadMessagesCount); + } + }); + } + isWebUserProfileLoaded = true; } catch (JSONException e) { Log.d(App.APP_LOG_TAG, e.getMessage()); isWebUserProfileLoaded = false; } + lastLoaded = System.currentTimeMillis(); return isWebUserProfileLoaded; } - /** - * Get the Avatar URL's - * @return Avatar URL's - * [0] small - * [1] medium - * [2] large + /* + // Getters & Setters */ - 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 String getAvatarUrl() { + return avatarUrl; } - public int getId(){ - try { - return json.getInt("id"); - } catch (JSONException e) { - return 0; - } + public String getGuid() { + return guid; } - /** - * Get the users profile address id - * @return guid - */ - public int getGuid(){ - try { - return json.getInt("guid"); - } catch (JSONException e) { - return 0; - } + public String getName() { + return name; } - public String getName(){ - try { - return json.getString("guid"); - } catch (JSONException e) { - return null; - } + public int getNotificationCount() { + return notificationCount; } - public String getDiasporaAddress(){ - try { - return json.getString("diaspora_id"); - } catch (JSONException e) { - return null; - } + public int getUnreadMessagesCount() { + return unreadMessagesCount; } - 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: + * string "diasporaAddress" + * int "id" * boolean "admin" + * int "following_count" * boolean "moderator" * array "aspects" * int "id" @@ -133,6 +156,12 @@ public class WebUserProfile { * ? ? * array "configured_services" * ? ? - * */ } + +interface WebUserProfileChangedListener { + void onUserProfileNameChanged(String name); + void onUserProfileAvatarChanged(String avatarUrl); + void onNotificationCountChanged(int notificationCount); + void onUnreadMessageCountChanged(int unreadMessageCount); +} diff --git a/app/src/main/java/de/baumann/diaspora/utils/AvatarImageLoader.java b/app/src/main/java/de/baumann/diaspora/utils/AvatarImageLoader.java new file mode 100644 index 00000000..83ba0e76 --- /dev/null +++ b/app/src/main/java/de/baumann/diaspora/utils/AvatarImageLoader.java @@ -0,0 +1,46 @@ +package de.baumann.diaspora.utils; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.widget.ImageView; + +import java.io.File; + +import de.baumann.diaspora.ImageDownloadTask; + +/** + * Created by Gregor Santner (de-live-gdev) on 24.03.16. + */ +public class AvatarImageLoader { + private File avatarFile; + + public AvatarImageLoader(Context context){ + avatarFile = new File(context.getFilesDir(), "avatar.png"); + } + + public void clearAvatarImage(){ + if (isAvatarDownloaded()) { + avatarFile.delete(); + } + } + + public boolean loadToImageView(ImageView imageView) { + if (avatarFile.exists()) { + Bitmap bitmap = BitmapFactory.decodeFile(avatarFile.getAbsolutePath()); + imageView.setImageBitmap(bitmap); + return true; + } + return false; + } + + public boolean isAvatarDownloaded() { + return avatarFile.exists(); + } + + public void startImageDownload(ImageView imageView, String avatarUrl) { + if(!avatarUrl.equals("")) { + new ImageDownloadTask(imageView, avatarFile.getAbsolutePath()).execute(avatarUrl); + } + } +} 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 d35bf424..7808f6f4 100644 --- a/app/src/main/java/de/baumann/diaspora/utils/Helpers.java +++ b/app/src/main/java/de/baumann/diaspora/utils/Helpers.java @@ -62,15 +62,6 @@ public class Helpers { "})();"); } - public static void getProfileId(final WebView wv) { - wv.loadUrl("javascript: ( function() {" + - " if (typeof gon !== 'undefined' && typeof gon.user !== 'undefined' && typeof gon.user.guid !== 'undefined') {" + - " var guid = gon.user.guid;" + - " AndroidBridge.setProfileId(guid.toString());" + - " } " + - "})();"); - } - public static void getUserProfile(final WebView wv) { wv.loadUrl("javascript: ( function() {" + " if (typeof gon !== 'undefined' && typeof gon.user !== 'undefined') {" + diff --git a/app/src/main/res/layout/activity_pods.xml b/app/src/main/res/layout/activity_pods.xml index 5e10d409..ff07f7e2 100644 --- a/app/src/main/res/layout/activity_pods.xml +++ b/app/src/main/res/layout/activity_pods.xml @@ -13,7 +13,7 @@ android:id="@+id/lstPods" android:choiceMode="singleChoice" android:layout_below="@+id/edtFilter" - android:layout_above="@+id/textView" /> + android:layout_above="@+id/navheader_description" /> diff --git a/app/src/main/res/layout/app_bar_main.xml b/app/src/main/res/layout/app_bar_main.xml index f7f7d128..f09539b6 100644 --- a/app/src/main/res/layout/app_bar_main.xml +++ b/app/src/main/res/layout/app_bar_main.xml @@ -25,13 +25,14 @@ + android:scaleType="centerCrop" + android:src="@drawable/header" /> + android:layout_alignParentStart="true" + android:layout_marginEnd="@dimen/activity_horizontal_margin" + android:layout_marginStart="@dimen/activity_horizontal_margin" + android:layout_marginTop="@dimen/nav_header_vertical_spacing" + android:src="@drawable/ic_launcher" /> + android:textSize="16sp" /> + android:layout_alignParentStart="true" + android:paddingBottom="@dimen/activity_horizontal_margin" + android:paddingEnd="@dimen/activity_horizontal_margin" + android:paddingStart="@dimen/activity_horizontal_margin" + android:text="@string/app_copyright" + android:textColor="@color/white" />