mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-18 03:39:00 +00:00
add sharing for level up and pet hatching
This commit is contained in:
parent
a28440c708
commit
5dc6ef5d91
7 changed files with 110 additions and 5 deletions
|
|
@ -116,6 +116,16 @@
|
|||
android:theme="@android:style/Theme.Translucent.NoTitleBar"
|
||||
android:label="@string/app_name" />
|
||||
<receiver android:process=":remote" android:name=".NotificationPublisher" />
|
||||
|
||||
<provider
|
||||
android:name="android.support.v4.content.FileProvider"
|
||||
android:authorities="com.habitrpg.android.habitica.fileprovider"
|
||||
android:grantUriPermissions="true"
|
||||
android:exported="false">
|
||||
<meta-data
|
||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||
android:resource="@xml/filepaths" />
|
||||
</provider>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
|
|||
|
|
@ -339,5 +339,8 @@ To start, which parts of your life do you want to improve?</string>
|
|||
<string name="user_id">User ID</string>
|
||||
<string name="email">Email</string>
|
||||
<string name="invite_users">Invite to Party</string>
|
||||
<string name="share_using">Share using</string>
|
||||
<string name="share_levelup" formatted="false">I just reached level %s on habitica.com !</string>
|
||||
<string name="share_hatched" formatted="false">I hatched a %1$s %2$s on habitica.com</string>
|
||||
|
||||
</resources>
|
||||
|
|
|
|||
4
Habitica/res/xml/filepaths.xml
Normal file
4
Habitica/res/xml/filepaths.xml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths>
|
||||
<files-path name="shared_images" path="shared_images/" />
|
||||
</paths>
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.habitrpg.android.habitica.events;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
|
||||
public class ShareEvent {
|
||||
|
||||
public String sharedMessage;
|
||||
public Bitmap shareImage;
|
||||
}
|
||||
|
|
@ -5,13 +5,22 @@ import android.app.PendingIntent;
|
|||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.database.sqlite.SQLiteDoneException;
|
||||
import android.databinding.DataBindingUtil;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.design.widget.TabLayout;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.support.v4.content.FileProvider;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
|
|
@ -41,6 +50,7 @@ import com.habitrpg.android.habitica.events.DisplayFragmentEvent;
|
|||
import com.habitrpg.android.habitica.events.DisplayTutorialEvent;
|
||||
import com.habitrpg.android.habitica.events.ReloadContentEvent;
|
||||
import com.habitrpg.android.habitica.events.SelectClassEvent;
|
||||
import com.habitrpg.android.habitica.events.ShareEvent;
|
||||
import com.habitrpg.android.habitica.events.TaskRemovedEvent;
|
||||
import com.habitrpg.android.habitica.events.ToggledInnStateEvent;
|
||||
import com.habitrpg.android.habitica.events.commands.BuyRewardCommand;
|
||||
|
|
@ -60,7 +70,9 @@ import com.habitrpg.android.habitica.ui.UiUtils;
|
|||
import com.habitrpg.android.habitica.ui.fragments.BaseMainFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.GemsPurchaseFragment;
|
||||
import com.habitrpg.android.habitica.ui.helpers.DataBindingUtils;
|
||||
import com.habitrpg.android.habitica.userpicture.BitmapUtils;
|
||||
import com.habitrpg.android.habitica.userpicture.UserPicture;
|
||||
import com.habitrpg.android.habitica.userpicture.UserPictureRunnable;
|
||||
import com.magicmicky.habitrpgwrapper.lib.models.ContentResult;
|
||||
import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser;
|
||||
import com.magicmicky.habitrpgwrapper.lib.models.SuppressedModals;
|
||||
|
|
@ -105,6 +117,9 @@ import org.json.JSONObject;
|
|||
import org.solovyev.android.checkout.ActivityCheckout;
|
||||
import org.solovyev.android.checkout.Checkout;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
|
@ -812,12 +827,26 @@ public class MainActivity extends BaseActivity implements HabitRPGUserCallback.O
|
|||
ImageView petImageView = (ImageView) petWrapper.findViewById(R.id.pet_imageview);
|
||||
|
||||
DataBindingUtils.loadImage(petImageView, "Pet-" + event.usingEgg.getKey() + "-" + event.usingHatchingPotion.getKey());
|
||||
|
||||
String potionName = event.usingHatchingPotion.getText();
|
||||
String eggName = event.usingEgg.getText();
|
||||
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
|
||||
.setTitle(getString(R.string.hatched_pet_title, event.usingHatchingPotion.getText(), event.usingEgg.getText()))
|
||||
.setTitle(getString(R.string.hatched_pet_title, potionName, eggName))
|
||||
.setView(petWrapper)
|
||||
.setPositiveButton(R.string.close, (dialog1, which) -> {
|
||||
dialog1.dismiss();
|
||||
.setPositiveButton(R.string.close, (hatchingDialog, which) -> {
|
||||
hatchingDialog.dismiss();
|
||||
})
|
||||
.setNeutralButton(R.string.share, (hatchingDialog, which) -> {
|
||||
ShareEvent event = new ShareEvent();
|
||||
event.sharedMessage = getString(R.string.share_hatched, potionName, eggName);
|
||||
Bitmap animalBitmap = ((BitmapDrawable)petImageView.getDrawable()).getBitmap();
|
||||
Bitmap sharedImage = Bitmap.createBitmap(140, 140, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(sharedImage);
|
||||
canvas.drawColor(getResources().getColor(R.color.brand_300));
|
||||
canvas.drawBitmap(animalBitmap, new Rect(0, 0, animalBitmap.getWidth(), animalBitmap.getHeight()),
|
||||
new Rect(30, 0, animalBitmap.getWidth() + 30, animalBitmap.getHeight()), new Paint());
|
||||
event.shareImage = sharedImage;
|
||||
EventBus.getDefault().post(event);
|
||||
hatchingDialog.dismiss();
|
||||
})
|
||||
.create();
|
||||
dialog.show();
|
||||
|
|
@ -825,7 +854,6 @@ public class MainActivity extends BaseActivity implements HabitRPGUserCallback.O
|
|||
|
||||
@Override
|
||||
public void onUserFail() {
|
||||
|
||||
}
|
||||
}, this.user));
|
||||
}
|
||||
|
|
@ -1004,6 +1032,17 @@ public class MainActivity extends BaseActivity implements HabitRPGUserCallback.O
|
|||
.setPositiveButton(R.string.levelup_button, (dialog, which) -> {
|
||||
checkClassSelection();
|
||||
})
|
||||
.setNeutralButton(R.string.share, (dialog, which) -> {
|
||||
ShareEvent event = new ShareEvent();
|
||||
event.sharedMessage = getString(R.string.share_levelup, level);
|
||||
UserPicture picture = new UserPicture(this, true, true);
|
||||
picture.setUser(this.user);
|
||||
picture.setPictureWithRunnable(avatarBitmap -> {
|
||||
event.shareImage = avatarBitmap;
|
||||
EventBus.getDefault().post(event);
|
||||
dialog.dismiss();
|
||||
});
|
||||
})
|
||||
.create();
|
||||
|
||||
alert.show();
|
||||
|
|
@ -1144,4 +1183,20 @@ public class MainActivity extends BaseActivity implements HabitRPGUserCallback.O
|
|||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void shareEvent(ShareEvent event) {
|
||||
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
|
||||
sharingIntent.setType("*/*");
|
||||
sharingIntent.putExtra(Intent.EXTRA_TEXT, event.sharedMessage);
|
||||
File f = BitmapUtils.saveToShareableFile(getFilesDir() + "/shared_images", "share.png", event.shareImage);
|
||||
Uri fileUri = FileProvider.getUriForFile(this, "com.habitrpg.android.habitica.fileprovider", f);
|
||||
sharingIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
|
||||
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(sharingIntent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
for (ResolveInfo resolveInfo : resInfoList) {
|
||||
String packageName = resolveInfo.activityInfo.packageName;
|
||||
this.grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
}
|
||||
startActivity(Intent.createChooser(sharingIntent, getString(R.string.share_using)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,23 @@ public class BitmapUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static File saveToShareableFile(String directory, String filename, Bitmap bmp) {
|
||||
try {
|
||||
File myDir = new File(directory);
|
||||
boolean res = myDir.mkdirs();
|
||||
|
||||
filename = directory + "/" + filename;
|
||||
|
||||
FileOutputStream out = new FileOutputStream(filename);
|
||||
bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
|
||||
out.flush();
|
||||
out.close();
|
||||
return new File(filename);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean hasSDCard() {
|
||||
String status = Environment.getExternalStorageState();
|
||||
return status.equals(Environment.MEDIA_MOUNTED);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import android.graphics.Canvas;
|
|||
import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
|
|
@ -14,6 +15,7 @@ import com.magicmicky.habitrpgwrapper.lib.models.HabitRPGUser;
|
|||
import com.squareup.picasso.Picasso;
|
||||
import com.squareup.picasso.Target;
|
||||
|
||||
import java.net.URI;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -128,6 +130,10 @@ public class UserPicture {
|
|||
generateImage(layerNames);
|
||||
}
|
||||
|
||||
public Uri getCachedFileUri() {
|
||||
return Uri.parse(BitmapUtils.getSavePath() + "/" + currentCacheFileName);
|
||||
}
|
||||
|
||||
private void clearRunning() {
|
||||
for (SpriteTarget target : this.targets) {
|
||||
Picasso.with(this.context).cancelRequest(target);
|
||||
|
|
|
|||
Loading…
Reference in a new issue