Skip to content

Commit

Permalink
Add support for shaarli2mastodon (#38)
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
angristan authored and dimtion committed Apr 14, 2019
1 parent 465d7bc commit 97fb5a8
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 25 deletions.
21 changes: 7 additions & 14 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,33 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/generated/not_namespaced_r_class_sources" />
<excludeFolder url="file://$MODULE_DIR$/build/generated/source/r" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotation_processor_list" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/apk_list" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/app_classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/build-info" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundle_manifest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/check-libraries" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/check-manifest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/checkDebugClasspath" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/check_manifest_result" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/compatible_screen_manifest" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/external_libs_dex" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-runtime-classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-verifier" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-apk" />
Expand Down Expand Up @@ -127,9 +122,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/signing_config" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/split-apk" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/split_list" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/tmp" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/validate_signing_config" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private void saveSettings() {
boolean isAutoDescription = ((CheckBox) findViewById(R.id.auto_load_description)).isChecked();
boolean isHandlingHttpScheme = ((CheckBox) findViewById(R.id.handle_http_scheme)).isChecked();
boolean useShaarli2twitter = ((CheckBox) findViewById(R.id.handle_twitter_plugin)).isChecked();
boolean useShaarli2mastodon = ((CheckBox) findViewById(R.id.handle_mastodon_plugin)).isChecked();

// Save data :
SharedPreferences pref = getSharedPreferences(getString(R.string.params), MODE_PRIVATE);
Expand All @@ -92,6 +93,7 @@ private void saveSettings() {
.putBoolean(getString(R.string.p_auto_title), isAutoTitle)
.putBoolean(getString(R.string.p_auto_description), isAutoDescription)
.putBoolean(getString(R.string.p_shaarli2twitter), useShaarli2twitter)
.putBoolean(getString(R.string.p_shaarli2mastodon), useShaarli2mastodon)
.apply();

setHandleHttpScheme(isHandlingHttpScheme);
Expand Down Expand Up @@ -120,6 +122,7 @@ private void loadSettings() {
boolean isAutoDescription = pref.getBoolean(getString(R.string.p_auto_description), false);
boolean isHandlingHttpScheme = isHandlingHttpScheme();
boolean isShaarli2twitter = pref.getBoolean(getString(R.string.p_shaarli2twitter), false);
boolean isShaarli2mastodon = pref.getBoolean(getString(R.string.p_shaarli2mastodon), false);

// Retrieve interface :
CheckBox privateCheck = findViewById(R.id.default_private);
Expand All @@ -128,6 +131,7 @@ private void loadSettings() {
CheckBox autoDescriptionCheck = findViewById(R.id.auto_load_description);
CheckBox handleHttpSchemeCheck = findViewById(R.id.handle_http_scheme);
CheckBox shaarli2twitter = findViewById(R.id.handle_twitter_plugin);
CheckBox shaarli2mastodon = findViewById(R.id.handle_mastodon_plugin);

// Display user previous settings :
privateCheck.setChecked(prv);
Expand All @@ -136,6 +140,7 @@ private void loadSettings() {
handleHttpSchemeCheck.setChecked(isHandlingHttpScheme);
shareDialogCheck.setChecked(sherDial);
shaarli2twitter.setChecked(isShaarli2twitter);
shaarli2mastodon.setChecked(isShaarli2mastodon);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private void readIntent(@NonNull Intent intent) {
userPrefs.isPrivateShare(),
selectedAccount,
userPrefs.isTweet(),
userPrefs.isToot(),
null,
null
);
Expand Down Expand Up @@ -177,6 +178,15 @@ private void openDialog() {
} else {
tweetCheckBox.setVisibility(View.VISIBLE);
}

// Init the toot button if necessary:
Switch tootCheckBox = findViewById(R.id.toot);
tootCheckBox.setChecked(userPrefs.isToot());
if (!userPrefs.isToot()) {
tootCheckBox.setVisibility(View.GONE);
} else {
tootCheckBox.setVisibility(View.VISIBLE);
}
}

/**
Expand Down Expand Up @@ -420,6 +430,11 @@ private void updateTweet(boolean tweet) {
tweetCheck.setChecked(tweet);
}

private void updateToot(boolean toot) {
Checkable tootCheck = findViewById(R.id.toot);
tootCheck.setChecked(toot);
}

/**
* Load everithing from the interface and share the link
*/
Expand All @@ -432,6 +447,7 @@ private void saveAndShare() {
((Checkable) findViewById(R.id.private_share)).isChecked(),
(ShaarliAccount) ((Spinner) findViewById(R.id.chooseAccount)).getSelectedItem(),
((Checkable) findViewById(R.id.tweet)).isChecked(),
((Checkable) findViewById(R.id.toot)).isChecked(),
null,
null
);
Expand Down Expand Up @@ -466,6 +482,7 @@ private void sendLink(@NonNull Link link) {
networkIntent.putExtra("tags", link.getTags());
networkIntent.putExtra("privateShare", link.isPrivate());
networkIntent.putExtra("tweet", link.isTweet());
networkIntent.putExtra("toot", link.isToot());
networkIntent.putExtra("chosenAccountId", link.getAccount().getId());
networkIntent.putExtra(
NetworkService.EXTRA_MESSENGER,
Expand Down Expand Up @@ -530,6 +547,7 @@ public void handleMessage(Message msg) {
updateTags(defaults.getTags(), false);
updatePrivate(defaults.isPrivate());
updateTweet(defaults.isTweet());
updateToot(defaults.isToot());


// Show that we are editing an existing entry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class NetworkManager {
"meta[property=og:description]",
"meta[name=description]",
"meta[name=twitter:description]",
"meta[name=mastodon:description]",
};

private final String mShaarliUrl;
Expand Down Expand Up @@ -261,7 +262,7 @@ public Link prefetchLinkData(Link link) throws IOException {
* Assume being logged in
* TODO: use the prefetch function
*/
public void postLink(String sharedUrl, String sharedTitle, String sharedDescription, String sharedTags, boolean privateShare, boolean tweet)
public void postLink(String sharedUrl, String sharedTitle, String sharedDescription, String sharedTags, boolean privateShare, boolean tweet, boolean toot)
throws IOException {
String encodedShareUrl = URLEncoder.encode(sharedUrl, "UTF-8");
retrievePostLinkToken(encodedShareUrl);
Expand All @@ -282,6 +283,7 @@ public void postLink(String sharedUrl, String sharedTitle, String sharedDescript
.data("lf_description", sharedDescription);
if (privateShare) postPageConn.data("lf_private", "on");
if (tweet) postPageConn.data("tweet", "on");
if (toot) postPageConn.data("toot", "on");
postPageConn.execute(); // Then we post
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected void onHandleIntent(Intent intent) {
String tags = intent.getStringExtra("tags");
boolean isPrivate = intent.getBooleanExtra("privateShare", true);
boolean tweet = intent.getBooleanExtra("tweet", true);
boolean toot = intent.getBooleanExtra("toot", true);

if ("".equals(title) && this.loadedTitle != null) {
title = this.loadedTitle;
Expand All @@ -95,9 +96,9 @@ protected void onHandleIntent(Intent intent) {
mShaarliAccount = (accountId != -1 ? acs.getShaarliAccountById(accountId) : acs.getDefaultAccount());
} catch (Exception e) {
e.printStackTrace();
sendNotificationShareError(sharedUrl, title, description, tags, isPrivate, tweet);
sendNotificationShareError(sharedUrl, title, description, tags, isPrivate, tweet, toot);
}
postLink(sharedUrl, title, description, tags, isPrivate, tweet);
postLink(sharedUrl, title, description, tags, isPrivate, tweet, toot);
stopSelf();
break;
case INTENT_PREFETCH:
Expand Down Expand Up @@ -219,14 +220,14 @@ private Link prefetchLink(Link sharedLink) {
}


private void postLink(String sharedUrl, String title, String description, String tags, boolean privateShare, boolean tweet){
private void postLink(String sharedUrl, String title, String description, String tags, boolean privateShare, boolean tweet, boolean toot){
boolean posted = true; // Assume it is shared
try {
// Connect the user to the site :
NetworkManager manager = new NetworkManager(mShaarliAccount);
manager.setTimeout(MANAGER_TIMEOUT);
if(manager.retrieveLoginToken() && manager.login()) {
manager.postLink(sharedUrl, title, description, tags, privateShare, tweet);
manager.postLink(sharedUrl, title, description, tags, privateShare, tweet, toot);
} else {
mError = new Exception("Could not connect to the shaarli. Possibles causes : unhandled shaarli, bad username or password");
posted = false;
Expand All @@ -238,7 +239,7 @@ private void postLink(String sharedUrl, String title, String description, String
}

if (!posted) {
sendNotificationShareError(sharedUrl, title, description, tags, privateShare, tweet);
sendNotificationShareError(sharedUrl, title, description, tags, privateShare, tweet, toot);
} else {
mToastHandler.post(new DisplayToast(getString(R.string.add_success)));
Log.i("SUCCESS", "Success while sharing link");
Expand All @@ -255,7 +256,7 @@ private String[] getPageTitleAndDescription(String url){
return NetworkManager.loadTitleAndDescription(url);
}

private void sendNotificationShareError(String sharedUrl, String title, String description, String tags, boolean privateShare, boolean tweet){
private void sendNotificationShareError(String sharedUrl, String title, String description, String tags, boolean privateShare, boolean tweet, boolean toot){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
Expand All @@ -274,6 +275,7 @@ private void sendNotificationShareError(String sharedUrl, String title, String d
resultIntent.putExtra("tags", tags);
resultIntent.putExtra("privateShare", privateShare);
resultIntent.putExtra("tweet", tweet);
resultIntent.putExtra("toot", toot);
resultIntent.putExtra("chosenAccountId", this.mShaarliAccount.getId());

resultIntent.putExtra(Intent.EXTRA_TEXT, sharedUrl);
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/dimtion/shaarlier/utils/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ public class Link implements Serializable {
private boolean isPrivate;
private ShaarliAccount account;
private boolean tweet;
private boolean toot;

private String datePostLink;
private String token;

public Link(String url, String title, String description, String tags, boolean isPrivate, ShaarliAccount account, boolean tweet, String datePostLink, String token) {
public Link(String url, String title, String description, String tags, boolean isPrivate, ShaarliAccount account, boolean tweet, boolean toot, String datePostLink, String token) {
this.url = url;
this.title = title;
this.description = description;
this.tags = tags;
this.isPrivate = isPrivate;
this.account = account;
this.tweet = tweet;
this.toot = toot;
this.datePostLink = datePostLink;
this.token = token;
}
Expand All @@ -34,6 +36,7 @@ public Link(Link other) {
this.isPrivate = other.isPrivate;
this.account = other.account;
this.tweet = other.tweet;
this.toot = other.toot;
this.datePostLink = other.datePostLink;
this.token = other.token;
}
Expand Down Expand Up @@ -94,6 +97,14 @@ public void setTweet(boolean tweet) {
this.tweet = tweet;
}

public boolean isToot() {
return toot;
}

public void setToot(boolean toot) {
this.toot = toot;
}

public String getDatePostLink() {
return datePostLink;
}
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/com/dimtion/shaarlier/utils/UserPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class UserPreferences {
private boolean autoTitle;
private boolean autoDescription;
private boolean tweet;
private boolean toot;

public static UserPreferences load(Activity a) {
UserPreferences p = new UserPreferences();
Expand All @@ -22,6 +23,7 @@ public static UserPreferences load(Activity a) {
p.autoTitle = pref.getBoolean(a.getString(R.string.p_auto_title), true);
p.autoDescription = pref.getBoolean(a.getString(R.string.p_auto_description), false);
p.tweet = pref.getBoolean(a.getString(R.string.p_shaarli2twitter), false);
p.toot = pref.getBoolean(a.getString(R.string.p_shaarli2mastodon), false);

return p;
}
Expand Down Expand Up @@ -58,12 +60,18 @@ public void setAutoDescription(boolean m_autoDescription) {
this.autoDescription = m_autoDescription;
}

public boolean isTweet() {
return tweet;
}
public boolean isTweet() { return tweet; }

public void setTweet(boolean tweet) {
this.tweet = tweet;
}

public boolean isToot() {
return toot;
}

public void setToot(boolean toot) {
this.toot = toot;
}
}

7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
android:checked="false"
android:text="@string/use_twitter" />

<CheckBox
android:id="@+id/handle_mastodon_plugin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:checked="false"
android:text="@string/use_mastodon" />

<!-- Footer -->
<TextView
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_share.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
android:padding="16dp"
android:text="@string/tweet" />

<Switch
android:id="@+id/toot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/toot" />

</LinearLayout>

</android.support.constraint.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ Cette application est fièrement développée par dimtion, vous pouvez me trouve
<string name="version">"Version %1$s"</string>
<string name="advanced">Avancé</string>
<string name="tweet">Tweeter</string>
<string name="toot">Toot</string>
<string name="use_twitter">Twitter (nécessite shaarli2twitter)</string>
<string name="use_mastodon">Mastodon (nécessite shaarli2mastodon)</string>
<string name="action_open_shaarli">Ouvrir Shaarli</string>
<string name="is_not_new_link">edition</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@
<string name="handle_http_scheme">Show Shaarlier in the list of web browsers</string>
<string name="version">"Version %1$s"</string>
<string name="p_shaarli2twitter" translatable="false">com.dimtion.shaarlier.shaarli2twitter</string>
<string name="p_shaarli2mastodon" translatable="false">com.dimtion.shaarlier.shaarli2mastodon</string>
<string name="tweet">Tweet</string>
<string name="toot">Toot</string>
<string name="use_twitter">Use twitter (needs shaarli2twitter plugin)</string>
<string name="use_mastodon">Use mastodon (needs shaarli2mastodon plugin)</string>
<string name="advanced">Advanced</string>
<string name="action_open_shaarli">Open Shaarli</string>
<string name="is_not_new_link">editing</string>
Expand Down

0 comments on commit 97fb5a8

Please sign in to comment.