diff --git a/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java b/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java index fb5f85525e..c7b9c58a07 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java +++ b/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java @@ -9,6 +9,7 @@ import org.joinmastodon.android.api.ObjectValidationException; import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSessionManager; +import org.joinmastodon.android.fragments.ComposeFragment; import org.joinmastodon.android.fragments.HomeFragment; import org.joinmastodon.android.fragments.ProfileFragment; import org.joinmastodon.android.fragments.SplashFragment; @@ -56,6 +57,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState){ if(intent.getBooleanExtra("fromNotification", false) && intent.hasExtra("notification")){ Notification notification=Parcels.unwrap(intent.getParcelableExtra("notification")); showFragmentForNotification(notification, session.getID()); + }else if(intent.getBooleanExtra("compose", false)){ + showCompose(); } } } @@ -91,6 +94,8 @@ protected void onNewIntent(Intent intent){ fragment.setArguments(args); showFragmentClearingBackStack(fragment); } + }else if(intent.getBooleanExtra("compose", false)){ + showCompose(); } } @@ -115,4 +120,15 @@ private void showFragmentForNotification(Notification notification, String accou fragment.setArguments(args); showFragment(fragment); } + + private void showCompose(){ + AccountSession session=AccountSessionManager.getInstance().getLastActiveAccount(); + if(session==null || !session.activated) + return; + ComposeFragment compose=new ComposeFragment(); + Bundle composeArgs=new Bundle(); + composeArgs.putString("account", session.getID()); + compose.setArguments(composeArgs); + showFragment(compose); + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java b/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java index 49ea70a753..52c27480fe 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java @@ -2,15 +2,22 @@ import android.app.Activity; import android.app.NotificationManager; +import android.content.ComponentName; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.ShortcutInfo; +import android.content.pm.ShortcutManager; +import android.graphics.drawable.Icon; import android.net.Uri; import android.os.Build; import android.util.Log; import com.google.gson.JsonParseException; +import org.joinmastodon.android.BuildConfig; import org.joinmastodon.android.E; +import org.joinmastodon.android.MainActivity; import org.joinmastodon.android.MastodonApp; import org.joinmastodon.android.R; import org.joinmastodon.android.api.MastodonAPIController; @@ -90,6 +97,7 @@ private AccountSessionManager(){ } lastActiveAccountID=prefs.getString("lastActiveAccount", null); MastodonAPIController.runInBackground(()->readInstanceInfo(domains)); + maybeUpdateShortcuts(); } public void addAccount(Instance instance, Token token, Account self, Application app, boolean active){ @@ -102,6 +110,7 @@ public void addAccount(Instance instance, Token token, Account self, Application if(PushSubscriptionManager.arePushNotificationsAvailable()){ session.getPushSubscriptionManager().registerAccountForPush(null); } + maybeUpdateShortcuts(); } public synchronized void writeAccountsFile(){ @@ -181,6 +190,7 @@ public void removeAccount(String id){ NotificationManager nm=MastodonApp.context.getSystemService(NotificationManager.class); nm.deleteNotificationChannelGroup(id); } + maybeUpdateShortcuts(); } @NonNull @@ -395,6 +405,29 @@ public void updateAccountInfo(String id, Account account){ writeAccountsFile(); } + private void maybeUpdateShortcuts(){ + if(Build.VERSION.SDK_INT<26) + return; + ShortcutManager sm=MastodonApp.context.getSystemService(ShortcutManager.class); + if((sm.getDynamicShortcuts().isEmpty() || BuildConfig.DEBUG) && !sessions.isEmpty()){ + // There are no shortcuts, but there are accounts. Add a compose shortcut. + ShortcutInfo info=new ShortcutInfo.Builder(MastodonApp.context, "compose") + .setActivity(ComponentName.createRelative(MastodonApp.context, MainActivity.class.getName())) + .setShortLabel(MastodonApp.context.getString(R.string.new_post)) + .setIcon(Icon.createWithResource(MastodonApp.context, R.mipmap.ic_shortcut_compose)) + .setIntent(new Intent(MastodonApp.context, MainActivity.class) + .setAction(Intent.ACTION_MAIN) + .putExtra("compose", true)) + .build(); + sm.setDynamicShortcuts(Collections.singletonList(info)); + }else if(sessions.isEmpty()){ + // There are shortcuts, but no accounts. Disable existing shortcuts. + sm.disableShortcuts(Collections.singletonList("compose"), MastodonApp.context.getString(R.string.err_not_logged_in)); + }else{ + sm.enableShortcuts(Collections.singletonList("compose")); + } + } + private static class SessionsStorageWrapper{ public List accounts; } diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/views/ImageAttachmentFrameLayout.java b/mastodon/src/main/java/org/joinmastodon/android/ui/views/ImageAttachmentFrameLayout.java index dff3aa2478..0bfcbdb160 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/views/ImageAttachmentFrameLayout.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/views/ImageAttachmentFrameLayout.java @@ -36,7 +36,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); return; } - int w=Math.min(((View)getParent()).getMeasuredWidth()-horizontalInset, V.dp(MAX_WIDTH)); + int w=Math.min(((View)getParent()).getMeasuredWidth(), V.dp(MAX_WIDTH))-horizontalInset; int actualHeight=Math.round(tile.height/1000f*w)+V.dp(1)*(tile.rowSpan-1); int actualWidth=Math.round(tile.width/1000f*w); if(tile.startCol+tile.colSpan + + + + diff --git a/mastodon/src/main/res/mipmap-anydpi-v26/ic_shortcut_compose.xml b/mastodon/src/main/res/mipmap-anydpi-v26/ic_shortcut_compose.xml new file mode 100644 index 0000000000..3d2eb2be37 --- /dev/null +++ b/mastodon/src/main/res/mipmap-anydpi-v26/ic_shortcut_compose.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/mastodon/src/main/res/values-night/colors.xml b/mastodon/src/main/res/values-night/colors.xml new file mode 100644 index 0000000000..4f6465484c --- /dev/null +++ b/mastodon/src/main/res/values-night/colors.xml @@ -0,0 +1,5 @@ + + + @color/gray_700 + @color/primary_600 + \ No newline at end of file diff --git a/mastodon/src/main/res/values/colors.xml b/mastodon/src/main/res/values/colors.xml index 4bb1213f52..dac3a6167c 100644 --- a/mastodon/src/main/res/values/colors.xml +++ b/mastodon/src/main/res/values/colors.xml @@ -93,4 +93,7 @@ @color/warning_500 @color/primary_500 + + @color/gray_100 + @color/primary_700 \ No newline at end of file