Skip to content

Commit

Permalink
Lint and unit test fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay0701 committed Jun 5, 2021
1 parent 22460a5 commit 89fa147
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 120 deletions.
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
android:resizeableActivity="true"
android:supportsRtl="true"
>
<activity android:name=".DrawingActivity"></activity>
<activity android:name=".DrawingActivity" />
<activity
android:name="com.ichi2.anki.IntentHandler"
android:label="@string/app_name"
Expand Down
23 changes: 10 additions & 13 deletions AnkiDroid/src/main/java/com/ichi2/anki/DrawingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,16 @@ public boolean onCreateOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {

int itemId = item.getItemId();
switch (itemId) {
case R.id.action_save:
Timber.i("Drawing:: Save button pressed");
finishWithSuccess();
break;
case R.id.action_whiteboard_edit:
Timber.i("Drawing:: Pen Color button pressed");
if (mColorPalette.getVisibility() == View.GONE) {
mColorPalette.setVisibility(View.VISIBLE);
} else {
mColorPalette.setVisibility(View.GONE);
}
break;
if (itemId == R.id.action_save) {
Timber.i("Drawing:: Save button pressed");
finishWithSuccess();
} else if (itemId == R.id.action_whiteboard_edit) {
Timber.i("Drawing:: Pen Color button pressed");
if (mColorPalette.getVisibility() == View.GONE) {
mColorPalette.setVisibility(View.VISIBLE);
} else {
mColorPalette.setVisibility(View.GONE);
}
}
return super.onOptionsItemSelected(item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ichi2.anki.multimediacard.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
Expand All @@ -18,7 +19,6 @@
import com.ichi2.anki.cardviewer.CardAppearance;
import com.ichi2.anki.UIUtils;
import com.ichi2.anki.dialogs.DiscardChangesDialog;
import com.ichi2.anki.dialogs.WhiteBoardWidthDialog;
import com.ichi2.anki.multimediacard.IMultimediaEditableNote;
import com.ichi2.anki.multimediacard.fields.AudioRecordingField;
import com.ichi2.anki.multimediacard.fields.IField;
Expand Down Expand Up @@ -61,6 +61,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import androidx.annotation.CheckResult;
Expand Down Expand Up @@ -112,10 +113,10 @@ public class VisualEditorActivity extends AnkiActivity implements ColorPickerDia
private String[] mFields;
@NonNull
private SelectionType mSelectionType = SelectionType.REGULAR;
private AssetReader mAssetReader = new AssetReader(this);
private final AssetReader mAssetReader = new AssetReader(this);
//Unsure if this is needed, or whether getCol will block until onCollectionLoaded completes.
private boolean mHasLoadedCol;
private LargeObjectStorage mLargeObjectStorage = new LargeObjectStorage(this);
private final LargeObjectStorage mLargeObjectStorage = new LargeObjectStorage(this);


@Override
Expand Down Expand Up @@ -273,7 +274,7 @@ private void openWhiteBoard() {

private void openFontSelectionDialog() {
FontSelectionDialog fontSelectionDialog = new FontSelectionDialog(this, 20);
fontSelectionDialog.setOnFontSizeChanged(size ->{
fontSelectionDialog.setOnFontSizeChanged(size -> {
mWebView.setSelectedFontSize(size);
});
fontSelectionDialog.showFontSelectionDialog();
Expand Down Expand Up @@ -306,16 +307,19 @@ private void openAdvancedViewerForAddImage() {


private void openMultimediaEditor(IField field) {
IMultimediaEditableNote note = NoteService.createEmptyNote(getCol().getModels().get(mModelId));
note.setField(0, field);
Intent editCard = new Intent(this, MultimediaEditFieldActivity.class);
editCard.putExtra(MultimediaEditFieldActivity.EXTRA_FIELD_INDEX, 0);
editCard.putExtra(MultimediaEditFieldActivity.EXTRA_FIELD, field);
editCard.putExtra(MultimediaEditFieldActivity.EXTRA_WHOLE_NOTE, note);
startActivityForResultWithoutAnimation(editCard, REQUEST_MULTIMEDIA_EDIT);
IMultimediaEditableNote note = NoteService.createEmptyNote(Objects.requireNonNull(getCol().getModels().get(mModelId)));
if (note != null) {
note.setField(0, field);
Intent editCard = new Intent(this, MultimediaEditFieldActivity.class);
editCard.putExtra(MultimediaEditFieldActivity.EXTRA_FIELD_INDEX, 0);
editCard.putExtra(MultimediaEditFieldActivity.EXTRA_FIELD, field);
editCard.putExtra(MultimediaEditFieldActivity.EXTRA_WHOLE_NOTE, note);
startActivityForResultWithoutAnimation(editCard, REQUEST_MULTIMEDIA_EDIT);
}
}


@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean checkCollectionHasLoaded(@StringRes int resId) {
if (mModelId == 0 || !mHasLoadedCol) {
//Haven't loaded yet.
Expand All @@ -332,14 +336,14 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Timber.d("data was null");
return;
}
if (resultCode != RESULT_OK) {
return;
}
if (data.getExtras() == null) {
return;
}
switch (requestCode) {
case REQUEST_MULTIMEDIA_EDIT:
if (resultCode != RESULT_OK) {
return;
}
if (data.getExtras() == null) {
return;
}
IField field = (IField) data.getExtras().get(MultimediaEditFieldActivity.EXTRA_RESULT_FIELD);

if (field == null) {
Expand All @@ -356,12 +360,6 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
break;
case REQUEST_WHITE_BOARD_EDIT:
// receive image from drawing activity
if (resultCode != RESULT_OK) {
return;
}
if (data.getExtras() == null) {
return;
}
String savedWhiteBoardPath = (String) data.getExtras().get(DrawingActivity.EXTRA_RESULT_WHITE_BOARD);

if (savedWhiteBoardPath == null) {
Expand All @@ -388,17 +386,17 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
* @param uri The uri of the image to load
* @return HTML referring to the loaded image
*/
@SuppressWarnings("PointlessArithmeticExpression")
@Nullable
private String loadImageIntoCollection(Uri uri) throws IOException {
//noinspection PointlessArithmeticExpression
String fileName;
final int oneMegabyte = 1 * 1000 * 1000;
String filename = ContentResolverUtil.getFileName(getContentResolver(), uri);
InputStream fd = getContentResolver().openInputStream(uri);

Map.Entry<String, String> fileNameAndExtension = FileUtil.getFileNameAndExtension(filename);

if (checkFilename(fileNameAndExtension)) {
if (checkFilename(Objects.requireNonNull(fileNameAndExtension))) {
fileName = String.format("%s-name", fileNameAndExtension.getKey());
} else {
fileName = fileNameAndExtension.getKey();
Expand All @@ -412,6 +410,7 @@ private String loadImageIntoCollection(Uri uri) throws IOException {
if (bytesWritten > oneMegabyte) {
Timber.w("File was too large: %d bytes", bytesWritten);
UIUtils.showThemedToast(this, getString(R.string.note_editor_paste_too_large), false);
//noinspection ResultOfMethodCallIgnored
new File(tempFilePath).delete();
return null;
}
Expand Down Expand Up @@ -554,6 +553,7 @@ private void failStartingVisualEditor() {
}


@SuppressWarnings("unused")
private void cloze(int clozeId) {
mWebView.insertCloze(clozeId);
}
Expand All @@ -572,7 +572,7 @@ protected void onCollectionLoaded(Collection col) {

JSONObject model = col.getModels().get(mModelId);
String css = getModelCss(model);
if (Models.isCloze(model)) {
if (Models.isCloze(Objects.requireNonNull(model))) {
Timber.d("Cloze detected. Enabling Cloze button");
findViewById(R.id.editor_button_cloze).setVisibility(View.VISIBLE);
}
Expand Down Expand Up @@ -753,8 +753,9 @@ public void onColorSelected(int dialogId, int color) {
}
}

@SuppressLint("MutatingSharedPrefs")
private void savePresetColor(int color) {
if (!arrayContains(ColorPickerDialog.MATERIAL_COLORS, color)) {
if (!arrayContains(color)) {
// add the custom color to shared preferences so we can use it in the presets later
Set<String> presets = mPreferences.getStringSet("custom_colors", null);
if (presets == null) {
Expand All @@ -773,13 +774,11 @@ private String toHex(int color) {
}
}

private boolean arrayContains(int[] array, int value) {
if (array != null) {
int length = array.length;
for (int i = 0; i < length; i++) {
if (array[i] == value) {
return true;
}
private boolean arrayContains(int value) {
int length = ColorPickerDialog.MATERIAL_COLORS.length;
for (int i = 0; i < length; i++) {
if (ColorPickerDialog.MATERIAL_COLORS[i] == value) {
return true;
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.ichi2.anki.multimediacard.visualeditor;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
Expand Down Expand Up @@ -44,6 +45,7 @@ public FontSelectionDialog(@NonNull Context context, @NonNull int fontSize) {


public final SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@SuppressLint("SetTextI18n")
@Override
public void onProgressChanged(SeekBar seekBar, int value, boolean b) {
mFontSize = value;
Expand All @@ -62,6 +64,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {
}
};

@SuppressLint("SetTextI18n")
public void showFontSelectionDialog() {
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
Expand Down Expand Up @@ -102,7 +105,7 @@ public void showFontSelectionDialog() {
.show();
}

public void setOnFontSizeChanged(Consumer<Integer> c) {
public void setOnFontSizeChanged(@Nullable Consumer<Integer> c) {
this.mOnFontSizeChanged = c;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
import android.content.Context;
import android.util.AttributeSet;


import java.util.Locale;

import androidx.annotation.NonNull;
Expand All @@ -56,7 +57,7 @@ public SummerNoteVisualEditor(Context context, AttributeSet attrs, int defStyleA
}

@Override
public String getJsFunctionName(VisualEditorFunctionality functionality) {
public String getJsFunctionName(@NonNull VisualEditorFunctionality functionality) {
switch (functionality) {
case BOLD: return "setBold";
case ITALIC: return "setItalic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ of this software and associated documentation files (the "Software"), to deal

import com.ichi2.utils.FunctionalInterfaces.Consumer;

import java.nio.charset.StandardCharsets;
import java.util.Locale;

import androidx.annotation.CheckResult;
Expand Down Expand Up @@ -243,7 +244,7 @@ public void injectCss(String css) {
Timber.v("Applying CSS: %s", css);
//Snippet from https://stackoverflow.com/a/30018910
try {
byte[] data = css.getBytes("UTF-8");
byte[] data = css.getBytes(StandardCharsets.UTF_8);
String encoded = Base64.encodeToString(data, Base64.NO_WRAP);
// Base-64 decode in the browser.
this.execInternal("javascript:(function() {" +
Expand All @@ -254,7 +255,7 @@ public void injectCss(String css) {
"parent.appendChild(style)" +
"})()");
} catch (Exception e) {
e.printStackTrace();
Timber.w(e);
}
}

Expand Down Expand Up @@ -290,15 +291,15 @@ public void load() {


public static class ExecEscaped {
private final String escapedValue;
private final String mEscapedValue;

protected ExecEscaped(@Nullable String value) {
this.escapedValue = value;
this.mEscapedValue = value;
}

@Nullable
public String getEscapedValue() {
return escapedValue;
return mEscapedValue;
}

public static ExecEscaped fromString(String s) {
Expand Down
25 changes: 0 additions & 25 deletions AnkiDroid/src/main/res/drawable/ic_visibility_black_24dp.xml

This file was deleted.

21 changes: 0 additions & 21 deletions AnkiDroid/src/main/res/drawable/ic_visibility_white_24dp.xml

This file was deleted.

15 changes: 15 additions & 0 deletions AnkiDroid/src/main/res/drawable/ic_visual_editor_black.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
<!--
Copyright (c) 2020 David Allison <[email protected]>
This program 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.
This program 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
this program. If not, see <http://www.gnu.org/licenses/>.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
Expand Down
Loading

0 comments on commit 89fa147

Please sign in to comment.