content: package im.actor.sdk.controllers;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.net.Uri;
  import android.os.Environment;
  import android.util.Log;
  import android.view.View;
  import android.webkit.MimeTypeMap;
  
  import java.io.File;
  import java.io.FileOutputStream;
  
  import im.actor.core.entity.FileReference;
  import im.actor.core.entity.Peer;
  import im.actor.core.utils.IOUtils;
  import im.actor.sdk.controllers.contacts.AddContactActivity;
  import im.actor.sdk.controllers.pickers.TakePhotoActivity;
  import im.actor.sdk.controllers.conversation.ChatActivity;
  import im.actor.sdk.controllers.group.GroupInfoActivity;
  import im.actor.sdk.controllers.group.InviteLinkActivity;
  import im.actor.sdk.controllers.fragment.preview.PictureActivity;
  import im.actor.sdk.controllers.settings.EditAboutActivity;
  import im.actor.sdk.controllers.settings.EditNameActivity;
  
  public class Intents {
  
      public static final String EXTRA_FORWARD_TEXT = "forward_text";
  
      public static final String EXTRA_FORWARD_TEXT_RAW = "forward_text_raw";
  
      public static final String EXTRA_FORWARD_CONTENT = "forward_content";
  
      public static final String EXTRA_SHARE_USER = "share_user";
  
      public static final String EXTRA_UID = "uid";
  
      public static final String EXTRA_GROUP_ID = "group_id";
  
      public static final String EXTRA_CHAT_PEER = "chat_peer";
      public static final String EXTRA_CHAT_COMPOSE = "compose";
  
      public static final String EXTRA_EDIT_TYPE = "edit_type";
      public static final String EXTRA_EDIT_ID = "edit_id";
  
      public static final int RESULT_DELETE = 0;
      public static final int RESULT_IMAGE = 1;
  
      public static final String EXTRA_ALLOW_DELETE = "allow_delete";
      public static final String EXTRA_RESULT = "result";
      public static final String EXTRA_IMAGE = "image";
  
      public static Intent pickAvatar(boolean isAllowDelete, Context context) {
          return new Intent(context, TakePhotoActivity.class)
                  .putExtra(EXTRA_ALLOW_DELETE, isAllowDelete);
      }
  
      public static Intent editMyName(Context context) {
          return new Intent(context, EditNameActivity.class)
                  .putExtra(EXTRA_EDIT_TYPE, EditNameActivity.TYPE_ME)
                  .putExtra(EXTRA_EDIT_ID, 0);
      }
  
      public static Intent editUserName(int uid, Context context) {
          return new Intent(context, EditNameActivity.class)
                  .putExtra(EXTRA_EDIT_TYPE, EditNameActivity.TYPE_USER)
                  .putExtra(EXTRA_EDIT_ID, uid);
      }
  
      public static Intent editGroupTitle(int groupId, Context context) {
          return new Intent(context, EditNameActivity.class)
                  .putExtra(EXTRA_EDIT_TYPE, EditNameActivity.TYPE_GROUP)
                  .putExtra(EXTRA_EDIT_ID, groupId);
      }
  
      public static Intent editGroupTheme(int groupId, Context context) {
          return new Intent(context, EditNameActivity.class)
                  .putExtra(EXTRA_EDIT_TYPE, EditNameActivity.TYPE_GROUP_THEME)
                  .putExtra(EXTRA_EDIT_ID, groupId);
      }
  
      public static Intent editUserAbout(Context context) {
          return new Intent(context, EditAboutActivity.class)
                  .putExtra(EXTRA_EDIT_TYPE, EditAboutActivity.TYPE_ME)
                  .putExtra(EXTRA_EDIT_ID, 0);
      }
  
      public static Intent editUserNick(Context context) {
          return new Intent(context, EditNameActivity.class)
                  .putExtra(EXTRA_EDIT_TYPE, EditNameActivity.TYPE_NICK);
      }
  
      public static Intent editGroupAbout(int groupId, Context context) {
          return new Intent(context, EditAboutActivity.class)
                  .putExtra(EXTRA_EDIT_TYPE, EditAboutActivity.TYPE_GROUP)
                  .putExtra(EXTRA_EDIT_ID, groupId);
      }
  
      public static Intent openGroup(int chatId, Context context) {
          Intent res = new Intent(context, GroupInfoActivity.class);
          res.putExtra(EXTRA_GROUP_ID, chatId);
          return res;
      }
  
      public static Intent inviteLink(int chatId, Context context) {
          Intent res = new Intent(context, InviteLinkActivity.class);
          res.putExtra(EXTRA_GROUP_ID, chatId);
          return res;
      }
  
      public static Intent openDialog(Peer peer, boolean compose, Context context) {
          final Intent intent = new Intent(context, ChatActivity.class);
          intent.putExtra(EXTRA_CHAT_PEER, peer.getUnuqueId());
          intent.putExtra(EXTRA_CHAT_COMPOSE, compose);
          intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
          return intent;
      }
  
      public static Intent openPrivateDialog(int uid, boolean compose, Context context) {
          return openDialog(Peer.user(uid), compose, context);
      }
  
      public static Intent openGroupDialog(int chatId, boolean compose, Context context) {
          return openDialog(Peer.group(chatId), compose, context);
      }
  
      public static Intent call(long phone) {
          return call(phone + "");
      }
  
      public static Intent call(String phone) {
          return new Intent(Intent.ACTION_DIAL).setData(Uri.parse("tel:+" + phone));
      }
  
      public static Intent findContacts(Context context) {
          return new Intent(context, AddContactActivity.class);
      }
  
      // External intents
  
      private static Uri getAvatarUri(FileReference location) {
          return Uri.parse("content://im.actor.avatar/" + location.getFileId());
      }
  
      public static Intent openDoc(String fileName, String downloadFileName) {
          String mimeType = MimeTypeMap.getSingleton()
                  .getMimeTypeFromExtension(IOUtils.getFileExtension(fileName));
          if (mimeType == null) {
              mimeType = "*/*";
          }
  
          return new Intent(Intent.ACTION_VIEW)
                  .setDataAndType(Uri.fromFile(new File(downloadFileName)), mimeType);
      }
  
      public static Intent shareDoc(String fileName, String downloadFileName) {
          String mimeType = MimeTypeMap.getSingleton()
                  .getMimeTypeFromExtension(IOUtils.getFileExtension(fileName));
          if (mimeType == null) {
              mimeType = "*/*";
          }
  
          return new Intent(Intent.ACTION_SEND)
                  .putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(downloadFileName)))
                  .setType(mimeType);
      }
  
      public static Intent shareAvatar(FileReference location) {
          return new Intent(Intent.ACTION_SEND)
                  .putExtra(Intent.EXTRA_STREAM, getAvatarUri(location))
                  .setType("image/jpeg");
      }
  
      public static Intent openAvatar(FileReference location) {
          return new Intent(Intent.ACTION_VIEW)
                  .setDataAndType(getAvatarUri(location), "image/jpeg");
      }
  
      public static Intent setAsAvatar(FileReference location) {
          Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
          intent.setDataAndType(getAvatarUri(location), "image/jpg");
          intent.putExtra("mimeType", "image/jpg");
          return intent;
      }
  
      public static Intent pickFile(Context context) {
          return im.actor.sdk.controllers.pickers.Intents.pickFile(context);
      }
  
      public static Intent pickLocation(Context context) {
          return im.actor.sdk.controllers.pickers.Intents.pickLocation(context);
      }
  
      public static void openMedia(Activity activity, View photoView, String path, int senderId) {
          PictureActivity.launchPhoto(activity, photoView, path, senderId);
      }
  
      public static void savePicture(Context context, Bitmap bitmap) {
  
          File actorPicturesFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
          actorPicturesFolder = new File(actorPicturesFolder, "Actor");
          actorPicturesFolder.mkdirs();
          try {
              File pictureFile = new File(actorPicturesFolder, System.currentTimeMillis() + ".jpg");
              pictureFile.createNewFile();
  
  
              FileOutputStream ostream = new FileOutputStream(pictureFile);
              bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
              ostream.close();
  
  
              Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
              Uri contentUri = Uri.fromFile(pictureFile);
              mediaScanIntent.setData(contentUri);
              context.sendBroadcast(mediaScanIntent);
              Log.d("Picture saving", "Saved as " + pictureFile.getPath());
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
  
  }
  