-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial code for application addons
Contains the initial code to fetch and parse addons and also the basic UI to display the addons.
- Loading branch information
Showing
19 changed files
with
950 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
AnkiDroid/src/main/java/com/ichi2/anki/jsaddons/AddonDetailsDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/**************************************************************************************** | ||
* Copyright (c) 2022 Mani <[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/>. * | ||
****************************************************************************************/ | ||
|
||
package com.ichi2.anki.jsaddons | ||
|
||
import android.app.Dialog | ||
import android.os.Bundle | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.core.os.BundleCompat | ||
import androidx.core.os.bundleOf | ||
import androidx.fragment.app.DialogFragment | ||
import com.ichi2.anki.R | ||
import com.ichi2.utils.cancelable | ||
import com.ichi2.utils.create | ||
import com.ichi2.utils.customView | ||
import com.ichi2.utils.positiveButton | ||
|
||
/** | ||
* Shows all available details for the addon identified by the [AddonModel] passed as an argument. | ||
*/ | ||
class AddonDetailsDialog : DialogFragment() { | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
dialog?.window?.setLayout( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.WRAP_CONTENT | ||
) | ||
} | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
val addonModel = | ||
BundleCompat.getParcelable(requireArguments(), KEY_ADDON_MODEL, AddonModel::class.java) | ||
?: error("No addon identifier was provided!") | ||
val contentView = | ||
requireActivity().layoutInflater.inflate(R.layout.dialog_addon_details, null).apply { | ||
findViewById<TextView>(R.id.addon_name).text = addonModel.name | ||
findViewById<TextView>(R.id.addon_description).text = addonModel.description | ||
findViewById<TextView>(R.id.addon_type).text = addonModel.addonType | ||
findViewById<TextView>(R.id.addon_author).text = addonModel.author["name"] | ||
findViewById<TextView>(R.id.addon_version).text = addonModel.version | ||
findViewById<TextView>(R.id.addon_js_api_version).text = addonModel.ankidroidJsApi | ||
findViewById<TextView>(R.id.addon_license).text = addonModel.license | ||
findViewById<TextView>(R.id.addon_homepage).text = addonModel.homepage | ||
} | ||
return AlertDialog.Builder(requireContext()).create { | ||
customView(contentView) | ||
cancelable(true) | ||
positiveButton(R.string.close) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val KEY_ADDON_MODEL = "key_addon_model" | ||
|
||
fun newInstance(addonModel: AddonModel): AddonDetailsDialog = AddonDetailsDialog().apply { | ||
arguments = bundleOf(KEY_ADDON_MODEL to addonModel) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
AnkiDroid/src/main/java/com/ichi2/anki/jsaddons/AddonStorage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/**************************************************************************************** | ||
* Copyright (c) 2022 Mani <[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/>. * | ||
***************************************************************************************/ | ||
|
||
package com.ichi2.anki.jsaddons | ||
|
||
import android.content.Context | ||
import com.ichi2.anki.BackupManager | ||
import com.ichi2.anki.CollectionHelper | ||
import com.ichi2.annotations.NeedsTest | ||
import java.io.File | ||
|
||
/** | ||
* Implemented functions for getting addons related directory for current profile | ||
*/ | ||
@NeedsTest("Addons directory test") | ||
class AddonStorage(val context: Context) { | ||
private val currentAnkiDroidDirectory = CollectionHelper.getCurrentAnkiDroidDirectory(context) | ||
private val addonsHomeDir = File(currentAnkiDroidDirectory, "addons") | ||
|
||
/** | ||
* Get addons directory for current profile | ||
* e.g. AnkiDroid/addons/ | ||
*/ | ||
fun getCurrentProfileAddonDir(): File { | ||
if (!addonsHomeDir.exists()) { | ||
addonsHomeDir.mkdirs() | ||
} | ||
return addonsHomeDir | ||
} | ||
|
||
/** | ||
* Get addon's directory which contains packages and index.js files | ||
* e.g. AnkiDroid/addons/some-addon/ | ||
* | ||
* @param addonName | ||
* @return some-addon dir e.g. AnkiDroid/addons/some-addon/ | ||
*/ | ||
fun getSelectedAddonDir(addonName: String): File { | ||
return File(addonsHomeDir, addonName) | ||
} | ||
|
||
/** | ||
* Get package.json for selected addons | ||
* e.g. AnkiDroid/addons/some-addon/package/package.json | ||
* | ||
* @param addonName | ||
* @return package.json file | ||
*/ | ||
fun getSelectedAddonPackageJson(addonName: String): File { | ||
val addonPath = getSelectedAddonDir(addonName) | ||
return File(addonPath, "package/package.json") | ||
} | ||
|
||
/** | ||
* Remove selected addon in list view from addons directory | ||
* | ||
* @param addonName | ||
*/ | ||
fun deleteSelectedAddonPackageDir(addonName: String): Boolean { | ||
val dir = getSelectedAddonDir(addonName) | ||
return BackupManager.removeDir(dir) | ||
} | ||
} |
Oops, something went wrong.