From f40a566e105682758a10f6c6394ebb04d6363fbf Mon Sep 17 00:00:00 2001 From: tanvidadu Date: Wed, 23 May 2018 02:34:32 +0530 Subject: [PATCH] Added JavaDocs and improved quality --- .../main/java/fr/free/nrw/commons/Utils.java | 27 +++++- .../achievements/AchievementsActivity.java | 82 ++++++++++--------- .../mwapi/ApacheHttpClientMediaWikiApi.java | 18 ++-- .../free/nrw/commons/mwapi/MediaWikiApi.java | 3 +- .../commons/theme/NavigationBaseActivity.java | 3 +- 5 files changed, 83 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/Utils.java b/app/src/main/java/fr/free/nrw/commons/Utils.java index 91c23ce26..889cffc22 100644 --- a/app/src/main/java/fr/free/nrw/commons/Utils.java +++ b/app/src/main/java/fr/free/nrw/commons/Utils.java @@ -2,11 +2,13 @@ package fr.free.nrw.commons; import android.content.Context; import android.content.Intent; +import android.graphics.Bitmap; import android.net.Uri; import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.customtabs.CustomTabsIntent; import android.support.v4.content.ContextCompat; +import android.view.View; import android.widget.Toast; import org.apache.commons.codec.binary.Hex; @@ -58,6 +60,7 @@ public class Utils { /** * URL Encode an URL in UTF-8 format + * * @param url Unformatted URL * @return Encoded URL */ @@ -81,6 +84,7 @@ public class Utils { /** * Generates licence name with given ID + * * @param license License ID * @return Name of license */ @@ -106,7 +110,8 @@ public class Utils { /** * Fixing incorrect extension - * @param title File name + * + * @param title File name * @param extension Correct extension * @return File with correct extension */ @@ -127,6 +132,7 @@ public class Utils { /** * Tells whether dark theme is active or not + * * @param context Activity context * @return The state of dark theme */ @@ -146,7 +152,7 @@ public class Utils { StringBuilder stringBuilder = new StringBuilder(); try { - String[] command = new String[] {"logcat","-d","-v","threadtime"}; + String[] command = new String[]{"logcat", "-d", "-v", "threadtime"}; Process process = Runtime.getRuntime().exec(command); @@ -171,8 +177,7 @@ public class Utils { final String appPackageName = BuildConfig.class.getPackage().getName(); try { context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); - } - catch (android.content.ActivityNotFoundException anfe) { + } catch (android.content.ActivityNotFoundException anfe) { context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); } } @@ -194,4 +199,18 @@ public class Utils { customTabsIntent.launchUrl(context, url); } + /** + * To take screenshot of the screen and return it in Bitmap format + * + * @param view + * @return + */ + public static Bitmap getScreenShot(View view) { + View screenView = view.getRootView(); + screenView.setDrawingCacheEnabled(true); + Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); + screenView.setDrawingCacheEnabled(false); + return bitmap; + } + } diff --git a/app/src/main/java/fr/free/nrw/commons/achievements/AchievementsActivity.java b/app/src/main/java/fr/free/nrw/commons/achievements/AchievementsActivity.java index b489b7ea6..57187be51 100644 --- a/app/src/main/java/fr/free/nrw/commons/achievements/AchievementsActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/achievements/AchievementsActivity.java @@ -1,62 +1,61 @@ package fr.free.nrw.commons.achievements; import android.annotation.SuppressLint; +import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; -import android.os.Environment; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.Toolbar; import android.util.DisplayMetrics; import android.util.Log; import android.view.Menu; -import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; -import android.widget.Toast; -import android.widget.Toolbar; import org.json.JSONObject; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.util.Random; import javax.inject.Inject; import butterknife.BindView; import butterknife.ButterKnife; -import dagger.Binds; import fr.free.nrw.commons.R; +import fr.free.nrw.commons.Utils; import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.mwapi.MediaWikiApi; import fr.free.nrw.commons.theme.NavigationBaseActivity; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.schedulers.Schedulers; -import timber.log.Timber; /** - * activity for sharing feedback on uploaded activity + * activity for sharing feedback on uploaded activity */ public class AchievementsActivity extends NavigationBaseActivity { private static final double BADGE_IMAGE_WIDTH_RATIO = 0.4; private static final double BADGE_IMAGE_HEIGHT_RATIO = 0.3; - @BindView(R.id.achievement_badge) ImageView imageView; + @BindView(R.id.achievement_badge) + ImageView imageView; @BindView(R.id.achievement_level) TextView textView; - @BindView(R.id.toolbar) android.support.v7.widget.Toolbar toolbar; - @Inject SessionManager sessionManager; - @Inject MediaWikiApi mediaWikiApi; + @BindView(R.id.toolbar) + Toolbar toolbar; + @Inject + SessionManager sessionManager; + @Inject + MediaWikiApi mediaWikiApi; private CompositeDisposable compositeDisposable = new CompositeDisposable(); + /** * This method helps in the creation Achievement screen and * dynamically set the size of imageView @@ -82,13 +81,13 @@ public class AchievementsActivity extends NavigationBaseActivity { */ RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); - params.height = (int) (height*BADGE_IMAGE_HEIGHT_RATIO); - params.width = (int) (width*BADGE_IMAGE_WIDTH_RATIO); + params.height = (int) (height * BADGE_IMAGE_HEIGHT_RATIO); + params.width = (int) (width * BADGE_IMAGE_WIDTH_RATIO); imageView.setImageResource(R.drawable.featured); imageView.requestLayout(); setSupportActionBar(toolbar); - setUploadCount(); + setAchievements(); initDrawer(); } @@ -104,33 +103,21 @@ public class AchievementsActivity extends NavigationBaseActivity { int id = item.getItemId(); if (id == R.id.share_app_icon) { View rootView = getWindow().getDecorView().findViewById(android.R.id.content); - Bitmap screenShot = getScreenShot(rootView); + Bitmap screenShot = Utils.getScreenShot(rootView); shareScreen(screenShot); } return super.onOptionsItemSelected(item); } - /** - * To take screenshot of the screen and return it in Bitmap format - * @param view - * @return - */ - public static Bitmap getScreenShot(View view) { - View screenView = view.getRootView(); - screenView.setDrawingCacheEnabled(true); - Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); - screenView.setDrawingCacheEnabled(false); - return bitmap; - } - /** * To take bitmap and store it temporary storage and share it + * * @param bitmap */ - void shareScreen ( Bitmap bitmap){ + void shareScreen(Bitmap bitmap) { try { - File file = new File(this.getExternalCacheDir(),"screen.png"); + File file = new File(this.getExternalCacheDir(), "screen.png"); FileOutputStream fOut = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); @@ -146,18 +133,39 @@ public class AchievementsActivity extends NavigationBaseActivity { } } - private void setUploadCount() { + /** + * To call the API to get results in form Single + * which then calls parseJson when results are fetched + */ + private void setAchievements() { compositeDisposable.add(mediaWikiApi .getAchievements(sessionManager.getCurrentAccount().name) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( - jsonObject -> parseJson(jsonObject) + jsonObject -> parseJson(jsonObject) )); } - private void parseJson(JSONObject object){ - Log.i("json",object.toString()); + /** + * used to parse the JSONObject containing results + * + * @param object + */ + private void parseJson(JSONObject object) { + Log.i("json", object.toString()); + } + + /** + * Creates a way to change current activity to AchievementActivity + * + * @param context + */ + public static void startYourself(Context context) { + Intent intent = new Intent(context, AchievementsActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + context.startActivity(intent); } } diff --git a/app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java b/app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java index acda34cb5..0d95282ef 100644 --- a/app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java +++ b/app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java @@ -43,7 +43,6 @@ import java.util.concurrent.Callable; import fr.free.nrw.commons.BuildConfig; import fr.free.nrw.commons.Media; import fr.free.nrw.commons.PageTitle; -import fr.free.nrw.commons.achievements.Achievements; import fr.free.nrw.commons.category.CategoryImageUtils; import fr.free.nrw.commons.category.QueryContinue; import fr.free.nrw.commons.notification.Notification; @@ -223,7 +222,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi { @Override public boolean pageExists(String pageName) throws IOException { - return Double.parseDouble( api.action("query") + return Double.parseDouble(api.action("query") .param("titles", pageName) .get() .getString("/api/query/pages/page/@_idx")) != -1; @@ -473,6 +472,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi { * The method takes categoryName as input and returns a List of Media objects * It uses the generator query API to get the images in a category, 10 at a time. * Uses the query continue values for fetching paginated responses + * * @param categoryName Category name as defined on commons * @return */ @@ -524,6 +524,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi { * For APIs that return paginated responses, MediaWiki APIs uses the QueryContinue to facilitate fetching of subsequent pages * https://www.mediawiki.org/wiki/API:Raw_query_continue * After fetching images a page of image for a particular category, shared prefs are updated with the latest QueryContinue Values + * * @param keyword * @param queryContinue */ @@ -535,6 +536,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi { /** * Before making a paginated API call, this method is called to get the latest query continue values to be used + * * @param keyword * @return */ @@ -621,19 +623,25 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi { }); } + /** + * This takes userName as input, which is then used to fetch the feedback/achievements + * statistics using OkHttp and JavaRx. This function return JSONObject + * @param userName + * @return + */ @NonNull @Override public Single getAchievements(String userName) { final String fetchAchievementUrlTemplate = wikiMediaToolforgeUrl + "urbanecmbot/commonsmisc/feedback.py"; - return Single.fromCallable(()->{ + return Single.fromCallable(() -> { String url = String.format( Locale.ENGLISH, fetchAchievementUrlTemplate, new PageTitle(userName).getText()); HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); - urlBuilder.addQueryParameter("user",userName); - Log.i("url",urlBuilder.toString()); + urlBuilder.addQueryParameter("user", userName); + Log.i("url", urlBuilder.toString()); Request request = new Request.Builder() .url(urlBuilder.toString()) .build(); diff --git a/app/src/main/java/fr/free/nrw/commons/mwapi/MediaWikiApi.java b/app/src/main/java/fr/free/nrw/commons/mwapi/MediaWikiApi.java index a7f9f8dfd..5cb5c4591 100644 --- a/app/src/main/java/fr/free/nrw/commons/mwapi/MediaWikiApi.java +++ b/app/src/main/java/fr/free/nrw/commons/mwapi/MediaWikiApi.java @@ -10,7 +10,6 @@ import java.io.InputStream; import java.util.List; import fr.free.nrw.commons.Media; -import fr.free.nrw.commons.achievements.Achievements; import fr.free.nrw.commons.notification.Notification; import io.reactivex.Observable; import io.reactivex.Single; @@ -79,7 +78,7 @@ public interface MediaWikiApi { Single getUploadCount(String userName); @NonNull - Single getAchievements (String userName); + Single getAchievements(String userName); interface ProgressListener { void onProgress(long transferred, long total); diff --git a/app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java b/app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java index d046b895e..b9f5c9866 100644 --- a/app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java @@ -80,8 +80,7 @@ public abstract class NavigationBaseActivity extends BaseActivity @Override public void onClick(View v) { drawerLayout.closeDrawer(navigationView); - startActivityWithFlags(NavigationBaseActivity.this, AchievementsActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TOP, - Intent.FLAG_ACTIVITY_SINGLE_TOP); + AchievementsActivity.startYourself(NavigationBaseActivity.this); } }); }