Guest User

Untitled

a guest
Feb 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 62.67 KB | None | 0 0
  1. package se.meanalytics.loopme20.CreateNewLoop;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.database.Cursor;
  9. import android.graphics.Bitmap;
  10. import android.graphics.BitmapFactory;
  11. import android.graphics.Color;
  12. import android.graphics.Matrix;
  13. import android.graphics.Xfermode;
  14. import android.media.ExifInterface;
  15. import android.net.Uri;
  16. import android.os.Vibrator;
  17. import android.provider.MediaStore;
  18. import android.provider.OpenableColumns;
  19. import android.support.v7.app.AppCompatActivity;
  20. import android.os.Bundle;
  21. import android.support.v7.app.AlertDialog;
  22. import android.text.Editable;
  23. import android.text.TextWatcher;
  24.  
  25. import android.view.Gravity;
  26. import android.view.MotionEvent;
  27. import android.view.View;
  28. import android.view.animation.Animation;
  29. import android.view.animation.BounceInterpolator;
  30. import android.view.animation.ScaleAnimation;
  31. import android.widget.CheckBox;
  32. import android.widget.EditText;
  33. import android.widget.ImageView;
  34. import android.widget.LinearLayout;
  35. import android.widget.ProgressBar;
  36. import android.widget.RelativeLayout;
  37. import android.widget.TableLayout;
  38. import android.widget.TextView;
  39. import android.widget.Toast;
  40.  
  41. import com.google.android.gms.analytics.HitBuilders;
  42. import com.google.android.gms.analytics.Tracker;
  43. import com.loopj.android.http.JsonHttpResponseHandler;
  44.  
  45. import org.apache.commons.io.FilenameUtils;
  46.  
  47. import Models.Loop;
  48. import cz.msebera.android.httpclient.Header;
  49.  
  50. import org.json.JSONArray;
  51. import org.json.JSONException;
  52. import org.json.JSONObject;
  53.  
  54. import java.io.ByteArrayOutputStream;
  55. import java.io.File;
  56. import java.io.FileNotFoundException;
  57. import java.io.FileOutputStream;
  58. import java.io.IOException;
  59. import java.io.InputStream;
  60. import java.io.OutputStream;
  61. import java.text.DateFormat;
  62. import java.text.ParseException;
  63. import java.text.SimpleDateFormat;
  64. import java.util.ArrayList;
  65. import java.util.Calendar;
  66. import java.util.Date;
  67. import java.util.TimeZone;
  68.  
  69. import Models.Group;
  70. import Models.Tag;
  71. import Models.Task;
  72. import se.meanalytics.loopme20.R;
  73. import se.meanalytics.loopme20.fileManagement.FileManager;
  74. import se.meanalytics.loopme20.webStuff.WebApiMethods;
  75. import v3.Log;
  76. import v3.LoopMeApp;
  77.  
  78. public class CreateNewLoop extends AppCompatActivity implements View.OnClickListener {
  79.  
  80. // Google Analytics
  81. Tracker tracker;
  82.  
  83. private int selectedFeeling = -90; // must select feeling
  84. private static final int ENABLED = 255;
  85. private static final int DISABLED = 100;
  86. private boolean can_post_anonymous;
  87. private boolean isTagsAvailable = false;
  88. AlertDialog.Builder dialogBuilder;
  89.  
  90. public static final String TAG = "CreateNewLoop";
  91. public static final String GROUP_ANONYMOUS = "anony";
  92.  
  93. private ArrayList<String> selectedTagsArray;
  94. private ArrayList<String> roleTypeArray = new ArrayList<>();
  95. private Task preselectedTag;
  96.  
  97. private WebApiMethods mWebApiMethods;
  98.  
  99. private TextView recipients_textView;
  100. private TextView approve_textView;
  101. private TextView group_name;
  102. private TextView anonymous_text;
  103. private ImageView one;
  104. private ImageView two;
  105. private ImageView three;
  106. private ImageView four;
  107. private ImageView five;
  108. private ImageView back_button;
  109. private EditText input_message;
  110. private CheckBox anonymous_checkbox;
  111. private RelativeLayout send_loop_button;
  112. private LinearLayout linearTags;
  113. private LinearLayout anonymous_layout;
  114. private LinearLayout followup_body_layout;
  115. private ProgressBar loading_progressBar;
  116. private TextView can_read_TextView;
  117. private TextView can_read_and_approve_TextView;
  118. public File file1;
  119.  
  120. // TODO change this value depending on physical screen size in inch...
  121.  
  122. private static final int BIG_TEXT_SIZE = 13;
  123. private static final int MEDIUM_TEXT_SIZE = 10;
  124. private static final int SMALL_TEXT_SIZE = 9;
  125. public static final int BIG_TAG_COLUMS = 3;
  126.  
  127. //V3
  128. private Group group;
  129. private Task task;
  130.  
  131. private Tag tags;
  132. private LinearLayout task_description_layout;
  133. private RelativeLayout choose_task_layout;
  134. private RelativeLayout choose_task_button;
  135. private RelativeLayout change_task_button;
  136. private RelativeLayout can_be_approved;
  137. private TextView feeling_and_tags_text;
  138. private TextView task_name;
  139. private TextView task_description;
  140. private ArrayList<Task> taskList;
  141. private ArrayList<Tag> tagsList;
  142. private ImageView task_information_icon;
  143. private ImageView text_information_icon;
  144. private ImageView feeling_information_icon;
  145. private ImageView members_that_can_read_loop_information_icon;
  146. private LinearLayout feelings_layout;
  147. private ImageView ivFile;
  148. private RelativeLayout rlFile;
  149. private ImageView ivClose;
  150.  
  151. private TextView tvFollowupQuestion;
  152. private EditText etFollowupAnswer;
  153.  
  154. private RelativeLayout rlUploadImage;
  155. private TextView tvFilename;
  156. private ImageView ivAddFile, ivRemoveFile;
  157. private ProgressBar pbSendLoop;
  158. private TextView tvSendLoop;
  159.  
  160. private int loop_type;
  161. public static final int INFO_LOOP = 1;
  162.  
  163. private ArrayList<Tag> freeTargetList = new ArrayList<>();
  164. private ArrayList<Task> obligatoryTargetList = new ArrayList<>();
  165.  
  166. public File file;
  167. private Intent data;
  168. int REQUEST_CAMERA = 0, SELECT_PHOTO = 1, SELECT_FILE = 2;
  169. int imageWidth = 400, imageHeight = 400;
  170.  
  171.  
  172. @Override
  173. protected void onCreate(Bundle savedInstanceState) {
  174. super.onCreate(savedInstanceState);
  175. overridePendingTransition(R.anim.right_slide_in, R.anim.nothing);
  176. setContentView(R.layout.v3_create_loop);
  177.  
  178. // Google Analytics
  179. // Obtain the shared Tracker instance.
  180. LoopMeApp application = (LoopMeApp) getApplication();
  181. tracker = application.getDefaultTracker();
  182. // Google Analytics
  183.  
  184. selectedTagsArray = new ArrayList<>();
  185. can_be_approved = (RelativeLayout) findViewById(R.id.can_be_approved);
  186.  
  187. // Followup question stuff
  188. followup_body_layout = (LinearLayout) findViewById(R.id.followup_body_layout);
  189. this.tvFollowupQuestion = (TextView) findViewById(R.id.tvQuestion);
  190. this.etFollowupAnswer = (EditText) findViewById(R.id.followup_body);
  191. this.ivAddFile = (ImageView) findViewById(R.id.ivAddFile);
  192. this.ivRemoveFile = (ImageView) findViewById(R.id.ivRemoveFile);
  193. followup_body_layout.setVisibility(View.GONE);
  194.  
  195. mWebApiMethods = new WebApiMethods(this);
  196. dialogBuilder = new AlertDialog.Builder(this);
  197. loading_progressBar = (ProgressBar) findViewById(R.id.loading_progressBar);
  198.  
  199. group = (Group) getIntent().getSerializableExtra("group");
  200. Task recivedTask = (Task) getIntent().getSerializableExtra("task");
  201.  
  202.  
  203. if (recivedTask != null) {
  204. // Is target
  205. this.task = recivedTask;
  206. setChosenTarget(this.task);
  207.  
  208. if (!recivedTask.needsApproval)
  209. can_be_approved.setVisibility(View.GONE);
  210.  
  211.  
  212. } else {
  213. can_be_approved.setVisibility(View.GONE);
  214. }
  215.  
  216. // Get targets and tags from intent
  217. taskList = (ArrayList<Task>) getIntent().getSerializableExtra("taskList");
  218. tagsList = (ArrayList<Tag>) getIntent().getSerializableExtra("tagsList");
  219. if (taskList != null) {
  220.  
  221. obligatoryTargetList.addAll(taskList);
  222. }
  223.  
  224. if (tagsList != null) {
  225. freeTargetList.addAll(tagsList);
  226. }
  227.  
  228. loop_type = getIntent().getIntExtra("loop_type", 0);
  229. can_post_anonymous = getIntent().getBooleanExtra(GROUP_ANONYMOUS, false);
  230.  
  231. task_information_icon = (ImageView) findViewById(R.id.task_information_icon);
  232. task_information_icon.setOnClickListener(this);
  233. text_information_icon = (ImageView) findViewById(R.id.text_information_icon);
  234. text_information_icon.setOnClickListener(this);
  235. feeling_information_icon = (ImageView) findViewById(R.id.feeling_information_icon);
  236. feeling_information_icon.setOnClickListener(this);
  237. feelings_layout = (LinearLayout) findViewById(R.id.feelings_layout);
  238. feeling_and_tags_text = (TextView) findViewById(R.id.feeling_and_tags_text);
  239. members_that_can_read_loop_information_icon = (ImageView) findViewById(R.id.members_that_can_read_loop_information_icon);
  240. members_that_can_read_loop_information_icon.setOnClickListener(this);
  241.  
  242.  
  243. linearTags = (LinearLayout) findViewById(R.id.tags_layout);
  244. group_name = (TextView) findViewById(R.id.group_name);
  245. group_name.setText("" + group.name);
  246. send_loop_button = (RelativeLayout) findViewById(R.id.send_button);
  247. send_loop_button.setOnClickListener(this);
  248. task_description_layout = (LinearLayout) findViewById(R.id.task_description_layout);
  249. pbSendLoop = (ProgressBar) findViewById(R.id.pbSendLoop);
  250. pbSendLoop.setVisibility(View.GONE);
  251. tvSendLoop = (TextView) findViewById(R.id.tvSendLoop);
  252. task_description_layout.setVisibility(View.GONE);
  253. choose_task_layout = (RelativeLayout) findViewById(R.id.choose_task_layout);
  254. choose_task_button = (RelativeLayout) findViewById(R.id.choose_task_button);
  255. choose_task_button.setOnClickListener(this);
  256. change_task_button = (RelativeLayout) findViewById(R.id.change_task_button);
  257.  
  258. change_task_button.setOnClickListener(this);
  259. task_name = (TextView) findViewById(R.id.task_name);
  260. task_description = (TextView) findViewById(R.id.task_description);
  261. can_read_TextView = (TextView) findViewById(R.id.can_read_TextView);
  262. can_read_and_approve_TextView = (TextView) findViewById(R.id.can_read_and_approve_TextView);
  263.  
  264. TextView header_text = (TextView) findViewById(R.id.header_text);
  265.  
  266. // File uploads (Images, Videos, etc..)
  267. rlFile = (RelativeLayout) findViewById(R.id.rlFile);
  268. ivFile = (ImageView) findViewById(R.id.ivFile);
  269.  
  270. rlUploadImage = (RelativeLayout) findViewById(R.id.rlUploadImage);
  271. rlUploadImage.setVisibility(View.VISIBLE);
  272. tvFilename = (TextView) findViewById(R.id.tvUploadFile);
  273. ivClose = (ImageView) findViewById(R.id.ivClose);
  274. recipients_textView = (TextView) findViewById(R.id.member_type_that_can_see_loop);
  275. approve_textView = (TextView) findViewById(R.id.member_type_that_can_see_and_approve_loop);
  276. recipients_textView.setOnClickListener(this);
  277.  
  278. rlFile.setVisibility(View.GONE);
  279. rlUploadImage.setOnClickListener(new View.OnClickListener() {
  280. @Override
  281. public void onClick(View v) {
  282.  
  283. selectImage();
  284. }
  285. });
  286.  
  287. ivClose.setOnClickListener(new View.OnClickListener() {
  288. @Override
  289. public void onClick(View v) {
  290. ivFile.setImageDrawable(null);
  291. file = null;
  292. rlFile.setVisibility(View.GONE);
  293. rlUploadImage.setVisibility(View.VISIBLE);
  294. }
  295. });
  296.  
  297. String loopTypeStr = "reflection";
  298. if (loop_type == INFO_LOOP) {
  299. loopTypeStr = "info";
  300. header_text.setText(R.string._send_info);
  301. }
  302. //getMyRecipients(group._id, loopTypeStr); // Get the number of recipients for this looptype...
  303. recipients_textView.setText("");
  304. approve_textView.setText("");
  305. String roleName = "";
  306. String roleCanApprove = "";
  307. String recipientText = recipients_textView.getText().toString();
  308. if (!recipientText.equals("")) {
  309. recipientText += ", ";
  310. }
  311.  
  312. String approveText = approve_textView.getText().toString();
  313. if (!approveText.equals("")) {
  314. approveText += ", ";
  315. }
  316. for (int i = 0; i < group.roles.length; i++) {
  317. if (group.roles[i].permission.loops.view_others == true) {
  318.  
  319. if (group.roles[i].permission.reports.approve == true) {
  320. if (hasRoleMembers(group.roles[i]._id, group)) {
  321. roleCanApprove = group.roles[i].name;
  322. approveText += roleCanApprove + ", ";
  323. }
  324. }
  325.  
  326.  
  327. if (hasRoleMembers(group.roles[i]._id, group)) {
  328. roleName = group.roles[i].name;
  329. recipientText += roleName + ", ";
  330. }
  331.  
  332.  
  333. }
  334.  
  335. if (group.roles[i].isSubscriber) {
  336. for (int j = 0; j < group.members_.length; j++) {
  337. if (group.members_[j].subscriptions == null)
  338. continue;
  339. else {
  340. for (int k = 0; k < group.members_[j].subscriptions.length; k++)
  341. if (group.members_[j].subscriptions[k]._id != null) {
  342. if (LoopMeApp.myUser._id.equals(group.members_[j].subscriptions[k]._id)) {
  343. roleName = group.members_[j].user.name;
  344. recipientText += roleName + ", ";
  345. }
  346.  
  347.  
  348. }
  349. }
  350. }
  351. }
  352.  
  353. approve_textView.setText(approveText);
  354.  
  355.  
  356.  
  357. }
  358. recipientText=recipientText.substring(0,recipientText.length()-2);
  359. recipients_textView.setText(recipientText);
  360.  
  361.  
  362. anonymous_text = (TextView) findViewById(R.id.anonymous_text);
  363. anonymous_text.setText(R.string.anonymous);
  364. anonymous_checkbox = (CheckBox) findViewById(R.id.anonymous_checkbox);
  365. anonymous_layout = (LinearLayout) findViewById(R.id.anonymous_layout);
  366.  
  367. if (task == null) {
  368. if (can_post_anonymous) {
  369. anonymous_layout.setVisibility(View.VISIBLE);
  370. } else {
  371. anonymous_layout.setVisibility(View.INVISIBLE);
  372. anonymous_checkbox.setChecked(false);
  373. }
  374. } else {
  375.  
  376. anonymous_layout.setVisibility(View.GONE);
  377. feeling_and_tags_text.setText(R.string._feeling_and_tags);
  378. choose_task_layout.setVisibility(View.GONE);
  379. change_task_button.setVisibility(View.GONE);
  380. task_description_layout.setVisibility(View.VISIBLE);
  381. task_name.setText("" + task.name);
  382. task_description.setText("" + task.description);
  383.  
  384. }
  385.  
  386. one = (ImageView) findViewById(R.id.very_sad_smiley_icon);
  387. one.setOnClickListener(this);
  388. two = (ImageView) findViewById(R.id.sad_smiley_icon);
  389. two.setOnClickListener(this);
  390. three = (ImageView) findViewById(R.id.neutral_smiley_icon);
  391. three.setOnClickListener(this);
  392. four = (ImageView) findViewById(R.id.happy_smiley_icon);
  393. four.setOnClickListener(this);
  394. five = (ImageView) findViewById(R.id.very_happy_smiley_icon);
  395. five.setOnClickListener(this);
  396.  
  397.  
  398. back_button = (ImageView) findViewById(R.id.back_button);
  399. back_button.setOnClickListener(this);
  400.  
  401. setAlphaOnSendButton(DISABLED); //Disable the button so user gets notified when all necessary feilds are filled in
  402.  
  403. input_message = (EditText) findViewById(R.id.text_body);
  404.  
  405. TextWatcher tw = new TextWatcher() {
  406. public void afterTextChanged(Editable s) {
  407. }
  408.  
  409. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  410. }
  411.  
  412. public void onTextChanged(CharSequence s, int start, int before, int count) {
  413. enableButtonHelpMethod();
  414. }
  415. };
  416.  
  417. input_message.addTextChangedListener(tw);
  418. etFollowupAnswer.addTextChangedListener(tw);
  419.  
  420. input_message.setOnTouchListener(new View.OnTouchListener() {
  421. @Override
  422. public boolean onTouch(View v, MotionEvent event) {
  423. if (v.getId() == R.id.text_body) {
  424. v.getParent().requestDisallowInterceptTouchEvent(true);
  425. switch (event.getAction() & MotionEvent.ACTION_MASK) {
  426. case MotionEvent.ACTION_UP:
  427. v.getParent().requestDisallowInterceptTouchEvent(false);
  428. break;
  429. }
  430. }
  431. return false;
  432. }
  433. });
  434.  
  435. if (loop_type == INFO_LOOP) {
  436. anonymous_layout.setVisibility(View.GONE);
  437. feelings_layout.setVisibility(View.GONE);
  438. choose_task_layout.setVisibility(View.GONE);
  439. change_task_button.setVisibility(View.GONE);
  440. task_description_layout.setVisibility(View.GONE);
  441. followup_body_layout.setVisibility(View.GONE);
  442. feeling_and_tags_text.setVisibility(View.GONE);
  443. feeling_information_icon.setVisibility(View.GONE);
  444. can_read_TextView.setText(R.string._can_be_seen_by);
  445.  
  446. }
  447. if (loop_type != INFO_LOOP) {
  448. can_read_TextView.setText(R.string._can_be_seen_by);
  449. can_read_and_approve_TextView.setText(R.string._can_be_seen_and_approved_by);
  450.  
  451. getFreeAndObligatoryTargets();
  452. }
  453. }
  454.  
  455. //This is the handler that will manager to process the broadcast intent
  456. private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
  457. @Override
  458. public void onReceive(Context context, Intent intent) {
  459. //Vibrate
  460. Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
  461. // Vibrate for 100 milliseconds
  462. v.vibrate(100);
  463. }
  464. };
  465.  
  466. @Override
  467. public void onPause() {
  468. super.onPause();
  469. this.unregisterReceiver(mMessageReceiver);
  470. }
  471.  
  472. @Override
  473. protected void onResume() {
  474. super.onResume();
  475. this.registerReceiver(mMessageReceiver, new IntentFilter("unique_name"));
  476.  
  477. // Google Analytics
  478. tracker.setScreenName(TAG);
  479. tracker.send(new HitBuilders.ScreenViewBuilder().build());
  480. }
  481.  
  482. private void getFreeAndObligatoryTargets() {
  483. if (group.activeTask != null) {
  484. for (int i = 0; i < group.activeTask.length; i++) {
  485. obligatoryTargetList.add(group.activeTask[i]);
  486. //isTagsAvailable = true;
  487. }
  488.  
  489. } else {
  490. if (tagsList != null) {
  491. for (int i = 0; i < tagsList.size(); i++) {
  492. if (tagsList.get(i).status == 1) {
  493. freeTargetList.add(tagsList.get(i));
  494. isTagsAvailable = true;
  495.  
  496. }
  497. }
  498. }
  499. }
  500.  
  501.  
  502. drawAvailableTags();
  503. }
  504.  
  505. /* private void getMyRecipients(final String groupId, String loopTyp){ //MÅSTE FIXAS HOS SENO. INGEN LOOPTYPE SKA SKICKAS
  506. mWebApiMethods.(groupId, loopTyp, new JsonHttpResponseHandler() {
  507. @Override
  508. public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
  509. super.onSuccess(statusCode, headers, response);
  510. recipients_textView.setText("");
  511. try {
  512. for (int i = 0; i < response.length(); i++) {
  513. String roleId = response.getJSONObject(i).getString("_id");
  514. String roleName = response.getJSONObject(i).getString("name");
  515.  
  516.  
  517. String recipientText = recipients_textView.getText().toString();
  518. if (!recipientText.equals("")) {
  519. recipientText += ", ";
  520. }
  521. int count = group.getMembersCountByRole(roleId);
  522. recipientText += roleName + " (" + count + ")";
  523. recipients_textView.setText(recipientText);
  524. }
  525. } catch (JSONException e) {
  526. e.printStackTrace();
  527. }
  528. synchronized (this) {
  529. notify();
  530. }
  531. }
  532.  
  533. @Override
  534. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  535. super.onFailure(statusCode, headers, throwable, errorResponse);
  536. //TODO HANDLE THIS
  537. }
  538. });
  539. }*/
  540.  
  541. @Override
  542. public void onClick(View v) {
  543. switch (v.getId()) {
  544. case R.id.very_sad_smiley_icon:
  545. unselectSmileys();
  546. one.setImageResource(R.mipmap.v3_mood_very_sad_white_icon);
  547. one.setBackgroundColor(Color.parseColor("#E31B19"));
  548. one.setAlpha(0.8f);
  549. selectedFeeling = -2;
  550. break;
  551. case R.id.sad_smiley_icon:
  552. unselectSmileys();
  553. two.setImageResource(R.mipmap.v3_mood_sad_white_icon);
  554. two.setBackgroundColor(Color.parseColor("#e6a04e"));
  555. two.setAlpha(0.8f);
  556. selectedFeeling = -1;
  557. break;
  558. case R.id.neutral_smiley_icon:
  559. unselectSmileys();
  560. three.setImageResource(R.mipmap.v3_mood_neutral_white_icon);
  561. three.setBackgroundColor(Color.parseColor("#299BF2"));
  562. three.setAlpha(0.8f);
  563. selectedFeeling = 0;
  564. break;
  565. case R.id.happy_smiley_icon:
  566. unselectSmileys();
  567. four.setImageResource(R.mipmap.v3_mood_happy_white_icon);
  568. four.setBackgroundColor(Color.parseColor("#49d394"));
  569. four.setAlpha(0.8f);
  570. selectedFeeling = 1;
  571. break;
  572. case R.id.very_happy_smiley_icon:
  573. unselectSmileys();
  574. five.setImageResource(R.mipmap.v3_mood_very_happy_white_icon);
  575. five.setBackgroundColor(Color.parseColor("#14CC00"));
  576. five.setAlpha(0.8f);
  577. selectedFeeling = 2;
  578. break;
  579. case R.id.back_button:
  580. if (input_message.getText().toString().length() > 0) {
  581. confirmExitDialog();
  582. } else {
  583. finishWithResult(RESULT_CANCELED);
  584. }
  585. break;
  586. case R.id.send_button:
  587. sendLoopDialog();
  588. break;
  589. case R.id.member_type_that_can_see_loop:
  590. showMembersInDialogue(roleTypeArray);
  591. break;
  592. case R.id.choose_task_button:
  593. showTasksInDialogue(false);
  594. break;
  595. case R.id.change_task_button:
  596. showTasksInDialogue(true);
  597. break;
  598. case R.id.task_information_icon:
  599. showInformationDialogue("task");
  600. break;
  601. case R.id.text_information_icon:
  602. showInformationDialogue("text");
  603. break;
  604. case R.id.feeling_information_icon:
  605. showInformationDialogue("feeling");
  606. break;
  607. case R.id.members_that_can_read_loop_information_icon:
  608. showInformationDialogue("members");
  609. break;
  610. }
  611.  
  612. try {
  613. Object[] obj = (Object[]) v.getTag();
  614. String itemPressed = (String) obj[2];
  615. TextView tv = (TextView) obj[1];
  616. if (itemPressed != null) {
  617. if (!selectedTagsArray.contains(itemPressed)) {
  618. selectedTagsArray.add(itemPressed);
  619. v.setBackgroundResource(R.drawable.rounded_corner_tag);
  620. tv.setTextColor(Color.WHITE);
  621.  
  622. } else {
  623. selectedTagsArray.remove(itemPressed);
  624. v.setBackgroundResource(R.drawable.rounded_corner_tag_white);
  625. tv.setTextColor(Color.GRAY);
  626. }
  627.  
  628. ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.9f, 1, 0.9f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  629. scaleAnimation.setInterpolator(new BounceInterpolator());
  630. scaleAnimation.setDuration(500);
  631. v.startAnimation(scaleAnimation);
  632. }
  633. } catch (NullPointerException npe) {
  634. }
  635. enableButtonHelpMethod();
  636. }
  637.  
  638. public void setChosenTarget(Task task) {
  639. this.task = task;
  640. if (task != null && task.followupQuestion != null) {
  641. if (!task.followupQuestion.isEmpty()) {
  642. this.tvFollowupQuestion.setText(task.followupQuestion);
  643. this.followup_body_layout.setVisibility(View.VISIBLE);
  644. } else {
  645. this.followup_body_layout.setVisibility(View.GONE);
  646. }
  647. }
  648. }
  649.  
  650. public Task getChosenTask() {
  651. return this.task;
  652. }
  653.  
  654. public boolean hasRoleMembers(String roles, Group group) {
  655.  
  656. for (int j = 0; j < group.members_.length; j++) {
  657. if (roles.equals(group.members_[j].role)) {
  658. return true;
  659. }
  660.  
  661. }
  662. return false;
  663. }
  664.  
  665. private void showInformationDialogue(String type) {
  666. String information_text = "";
  667. String title_text = "";
  668. switch (type) {
  669. case "task":
  670. information_text = getString(R.string._expl_task);
  671. title_text = getString(R.string._expl_task_title);
  672. break;
  673. case "text":
  674. information_text = getString(R.string._expl_message);
  675. title_text = getString(R.string._expl_message_title);
  676. if (loop_type == INFO_LOOP) {
  677. information_text = getString(R.string._expl_message_info);
  678. title_text = getString(R.string._expl_message_title_info);
  679. }
  680. break;
  681. case "feeling":
  682. information_text = getString(R.string._expl_feeling_and_tags);
  683. title_text = getString(R.string._expl_feeling_and_tags_title);
  684. break;
  685. case "members":
  686. information_text = getString(R.string._expl_who_can_see);
  687. title_text = getString(R.string._expl_who_can_see_title);
  688. if (loop_type == INFO_LOOP) {
  689. information_text = getString(R.string._expl_who_can_see_info);
  690. title_text = getString(R.string._expl_who_can_see_title_info);
  691. }
  692. break;
  693. }
  694. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  695. builder.setTitle(title_text);
  696. builder.setMessage(information_text);
  697. builder.setPositiveButton(getString(R.string.OK), new DialogInterface.OnClickListener() {
  698. @Override
  699. public void onClick(DialogInterface dialog, int which) {
  700. //Do nothing
  701. }
  702. });
  703. builder.create().show();
  704. }
  705.  
  706. private void showTasksInDialogue(final Boolean change_back_to_free_loop) {
  707. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  708. builder.setTitle(getString(R.string._choose));
  709. int my_approved = 0;
  710. int my_pending = 0;
  711. int lenght = 0;
  712. if (group.activeTask != null) {
  713.  
  714. ArrayList<String> roles = new ArrayList<>();
  715.  
  716.  
  717. for (int i = 0; i < group.activeTask.length; i++) {
  718. if (group.activeTask[i].progress != null) {
  719. for (int j = 0; j < group.activeTask[i].progress.length; j++) {
  720. for (int k = 0; k < group.activeTask[i].progress[j].member.length; k++) {
  721. if (group.activeTask[i].progress[j].member[k].user.equals(group.myMember.user)) {
  722. my_approved = group.activeTask[i].progress[j].member[k].progress.approved;
  723. my_pending = group.activeTask[i].progress[j].member[k].progress.pending;
  724.  
  725.  
  726. }
  727.  
  728.  
  729. }
  730. }
  731. if (my_approved + my_pending != group.activeTask[i].count) {
  732.  
  733. DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  734. formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
  735. Date date = null;
  736. Date date1 = null;
  737.  
  738. try {
  739. String sd = formatter.format(Calendar.getInstance().getTime());
  740.  
  741. if (group.activeTask[i].deadline != null) {
  742. date = (Date) formatter.parse(group.activeTask[i].deadline);
  743. date1 = (Date) formatter.parse(sd);
  744.  
  745.  
  746. long timeLeft = date.getTime() - date1.getTime();
  747.  
  748.  
  749. if (timeLeft < 0 && group.activeTask[i].lateSubmissionsAllowed == false) {
  750. continue;
  751. } else roles.add(group.activeTask[i].name);
  752.  
  753. } else roles.add(group.activeTask[i].name);
  754.  
  755.  
  756. } catch (ParseException e) {
  757. e.printStackTrace();
  758. }
  759.  
  760.  
  761. }
  762.  
  763.  
  764. }
  765.  
  766.  
  767. }
  768.  
  769.  
  770. final CharSequence[] task = new CharSequence[roles.size()];
  771.  
  772.  
  773. if (change_back_to_free_loop) {
  774. task[0] = getString(R.string._free_loop);
  775.  
  776. for (int i = 0; i < roles.size(); i++) {
  777. task[i] = roles.get(i);
  778.  
  779. }
  780. } else {
  781. for (int i = 0; i < roles.size(); i++) {
  782.  
  783. task[i] = roles.get(i);
  784. }
  785.  
  786. }
  787.  
  788.  
  789. builder.setItems(task, new DialogInterface.OnClickListener() {
  790. @Override
  791. public void onClick(DialogInterface dialog, int which) {
  792. if (change_back_to_free_loop == false || (change_back_to_free_loop == true && which != 0)) {
  793. setChosenTarget(group.activeTask[which]);
  794. choose_task_layout.setVisibility(View.GONE);
  795. task_description_layout.setVisibility(View.VISIBLE);
  796. task_name.setText("" + getChosenTask().name);
  797. task_description.setText("" + getChosenTask().description);
  798.  
  799. if (group.activeTask[which].needsApproval == false)
  800. can_be_approved.setVisibility(View.GONE);
  801.  
  802.  
  803. anonymous_layout.setVisibility(View.GONE);
  804.  
  805. } else {
  806. setChosenTarget(null);
  807. choose_task_layout.setVisibility(View.VISIBLE);
  808. task_description_layout.setVisibility(View.GONE);
  809. if (can_post_anonymous == true) {
  810. anonymous_layout.setVisibility(View.VISIBLE);
  811. } else
  812. anonymous_layout.setVisibility(View.GONE);
  813. }
  814. enableButtonHelpMethod();
  815. }
  816. });
  817. builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
  818. @Override
  819. public void onClick(DialogInterface dialog, int which) {
  820. //Do nothing
  821. enableButtonHelpMethod();
  822. }
  823. });
  824. builder.create().show();
  825. } else {
  826. builder.setMessage(getString(R.string._there_are_no_tasks_in_this_group));
  827. builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
  828. @Override
  829. public void onClick(DialogInterface dialog, int which) {
  830. //Do nothing
  831. enableButtonHelpMethod();
  832. }
  833. });
  834. builder.create().show();
  835. }
  836. }
  837.  
  838. private void showMembersInDialogue(ArrayList<String> al) {
  839. String message = "";
  840.  
  841. if (al.size() != 0) {
  842. for (int i = 0; i < roleTypeArray.size(); i++) {
  843. message = message + roleTypeArray.get(i).toString() + "\n";
  844. }
  845. message = message.substring(0, message.length() - 1);
  846. dialogBuilder.setTitle(getString(R.string.recipients));
  847. dialogBuilder.setMessage(message);
  848. dialogBuilder.setPositiveButton(getString(R.string.OK), new DialogInterface.OnClickListener() {
  849. @Override
  850. public void onClick(DialogInterface dialog, int which) {
  851. //do nothing
  852. }
  853. });
  854. dialogBuilder.create().show();
  855. }
  856. }
  857.  
  858.  
  859. private void setAlphaOnSendButton(int alpha) {
  860. send_loop_button.getBackground().setAlpha(alpha);
  861. if (alpha == DISABLED) {
  862. send_loop_button.setClickable(false);
  863. } else {
  864. send_loop_button.setClickable(true);
  865. }
  866. }
  867.  
  868. // Reset the feelings
  869. private void unselectSmileys() {
  870. one.setImageResource(R.mipmap.v3_mood_very_sad_gray_icon);
  871. two.setImageResource(R.mipmap.v3_mood_sad_gray_icon);
  872. three.setImageResource(R.mipmap.v3_mood_neutral_gray_icon);
  873. four.setImageResource(R.mipmap.v3_mood_happy_gray_icon);
  874. five.setImageResource(R.mipmap.v3_mood_very_happy_gray_icon);
  875.  
  876. one.setBackgroundColor(Color.TRANSPARENT);
  877. two.setBackgroundColor(Color.TRANSPARENT);
  878. three.setBackgroundColor(Color.TRANSPARENT);
  879. four.setBackgroundColor(Color.TRANSPARENT);
  880. five.setBackgroundColor(Color.TRANSPARENT);
  881. }
  882.  
  883. private void drawAvailableTags() {
  884. Log.d(TAG, "drawAvailableTags");
  885. int itemCount = 0;
  886. linearTags.removeAllViews();
  887. int numberOfRows = freeTargetList.size() / BIG_TAG_COLUMS;
  888.  
  889. if ((freeTargetList.size() % BIG_TAG_COLUMS) != 0) // division is even
  890. numberOfRows++;
  891.  
  892. for (int i = 0; i < numberOfRows; i++) {
  893. LinearLayout lay = new LinearLayout(this);
  894. lay.setOrientation(LinearLayout.HORIZONTAL);
  895. LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 105);
  896. lay.setWeightSum(BIG_TAG_COLUMS);
  897. lay.setLayoutParams(LLParams);
  898.  
  899. for (int j = 0; j < BIG_TAG_COLUMS; j++) {
  900. TextView myTag = new TextView(this);
  901. myTag.setLayoutParams(new TableLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1f));
  902.  
  903.  
  904. if (preselectedTag != null && itemCount < freeTargetList.size()) {
  905. Log.d(TAG, "preselectedTag.name: " + preselectedTag);
  906. Log.d(TAG, "freeTargetList.get(itemCount).name: " + freeTargetList.get(itemCount).name);
  907. if (freeTargetList.get(itemCount)._id.equals(preselectedTag._id)) {
  908. selectedTagsArray.add(freeTargetList.get(itemCount)._id);
  909. }
  910. }
  911.  
  912. if (itemCount >= freeTargetList.size()) {
  913. myTag.setText("");
  914. lay.addView(myTag);
  915. itemCount++;
  916. } else {
  917. myTag.setGravity(Gravity.CENTER);
  918.  
  919. if (freeTargetList.get(itemCount).name.length() > 50) {
  920. myTag.setTextSize(SMALL_TEXT_SIZE);
  921. } else if (freeTargetList.get(itemCount).name.length() > 30) {
  922. myTag.setTextSize(MEDIUM_TEXT_SIZE);
  923. } else {
  924. myTag.setTextSize(BIG_TEXT_SIZE);
  925. }
  926.  
  927. if (selectedTagsArray.contains(freeTargetList.get(itemCount)._id)) {
  928. myTag.setBackgroundResource(R.drawable.rounded_corner_tag);
  929. myTag.setTextColor(Color.WHITE);
  930. } else {
  931. myTag.setBackgroundResource(R.drawable.rounded_corner_tag_white);
  932. }
  933. myTag.setText(freeTargetList.get(itemCount).name);
  934. myTag.setClickable(true);
  935. myTag.setOnClickListener(this);
  936. Object[] obj = new Object[3];
  937. obj[0] = freeTargetList.get(itemCount).name;
  938. obj[1] = myTag;
  939. obj[2] = freeTargetList.get(itemCount)._id;
  940. myTag.setTag(obj);
  941. lay.addView(myTag);
  942. itemCount++;
  943. }
  944. }
  945. linearTags.addView(lay);
  946. }
  947. }
  948.  
  949. @Override
  950. public void onBackPressed() {
  951. if (input_message.getText().toString().length() > 0) {
  952. confirmExitDialog();
  953. } else {
  954. finishWithResult(RESULT_CANCELED);
  955. super.onBackPressed();
  956. }
  957. }
  958.  
  959. private void confirmExitDialog() {
  960. DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
  961. @Override
  962. public void onClick(DialogInterface dialog, int which) {
  963. switch (which) {
  964. case DialogInterface.BUTTON_POSITIVE:
  965. finishWithResult(RESULT_CANCELED);
  966. break;
  967.  
  968. case DialogInterface.BUTTON_NEGATIVE:
  969. break;
  970. }
  971. }
  972. };
  973. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  974. builder.setMessage(getString(R.string.cancel_loop)).setPositiveButton(getString(R.string.yes), dialogClickListener)
  975. .setNegativeButton(getString(R.string.no), dialogClickListener).show();
  976. }
  977.  
  978. private static final int READ_REQUEST_CODE = 42;
  979.  
  980. private void selectImage() {
  981.  
  982. // TODO: TESTING
  983. // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
  984. // browser.
  985. //Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  986. // Filter to only show results that can be "opened", such as a
  987. // file (as opposed to a list of contacts or timezones)
  988. //intent.addCategory(Intent.CATEGORY_OPENABLE);
  989.  
  990. // Filter to show only images, using the image MIME data type.
  991. // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
  992. // To search for all documents available via installed storage providers,
  993. // it would be "*/*".
  994. //intent.setType("image/*");
  995. //startActivityForResult(intent, READ_REQUEST_CODE);
  996. // TODO: TESTING
  997.  
  998. // TODO: REMOVE "false" to make code work
  999. if (this.file == null) {
  1000. final CharSequence[] items = {getString(R.string._take_photo), getString(R.string._choose_photo), getString(R.string._choose_file),
  1001. getString(R.string._cancel)};
  1002. android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
  1003. builder.setTitle(R.string._add_photo);
  1004. builder.setItems(items, new DialogInterface.OnClickListener() {
  1005. @Override
  1006. public void onClick(DialogInterface dialog, int item) {
  1007. if (item == 0) {
  1008. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  1009. //Toast.makeText(getApplicationContext(), R.string._cannot_take_photos_yet, Toast.LENGTH_LONG).show();
  1010.  
  1011. startActivityForResult(intent, REQUEST_CAMERA);
  1012. } else if (item == 1) {
  1013. //userChoosenTask="Choose from Library";
  1014. Intent intent = new Intent();
  1015. //intent.setType("*/*");
  1016. intent.setType("image/*");
  1017. intent.setAction(Intent.ACTION_GET_CONTENT);//
  1018.  
  1019. //Toast.makeText(getApplicationContext(), R.string._cannot_upload_photos_yet, Toast.LENGTH_LONG).show();
  1020. startActivityForResult(Intent.createChooser(intent, "Select Photo"), SELECT_PHOTO); // COMMENT THIS BACK IN !!!!
  1021. } else if (item == 2) {
  1022. //userChoosenTask="Choose from Library";
  1023. Intent intent = new Intent();
  1024. intent.setType("*/*");
  1025. //intent.setType("image/*");
  1026. //Toast.makeText(getApplicationContext(), R.string._cannot_upload_files_yet, Toast.LENGTH_LONG).show();
  1027. intent.setAction(Intent.ACTION_GET_CONTENT);//
  1028. startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
  1029. } else {
  1030. dialog.dismiss();
  1031. }
  1032. }
  1033. });
  1034. builder.show();
  1035. } else {
  1036. this.file = null;
  1037. tvFilename.setText(getString(R.string._upload_file));
  1038. this.ivAddFile.setVisibility(View.VISIBLE);
  1039. this.ivRemoveFile.setVisibility(View.GONE);
  1040. }
  1041. }
  1042.  
  1043. String filename = null;
  1044.  
  1045. public File getFileByUri(Uri uri) {
  1046. Log.d("FILE_EXISTSSS", "getFileByUri");
  1047. try {
  1048. /*if (filesize >= FILE_SIZE_LIMIT) {
  1049. Toast.makeText(this,"The selected file is too large. Selet a new file with size less than 2mb",Toast.LENGTH_LONG).show();
  1050. } else {*/
  1051. String mimeType = getContentResolver().getType(uri);
  1052. if (mimeType == null) {
  1053. String path = FileManager.getPath(this, uri);
  1054. if (path == null) {
  1055. filename = FilenameUtils.getName(uri.toString());
  1056. } else {
  1057. File file = new File(path);
  1058. filename = file.getName();
  1059. }
  1060. Log.d("FILE_EXISTSSS", "mimeType");
  1061. } else {
  1062. Uri returnUri = uri;
  1063. Cursor returnCursor = getContentResolver().query(returnUri, null, null, null, null);
  1064. int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
  1065. int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
  1066. returnCursor.moveToFirst();
  1067. filename = returnCursor.getString(nameIndex);
  1068. String size = Long.toString(returnCursor.getLong(sizeIndex));
  1069. Log.d("FILE_EXISTSSS", "returnUri");
  1070. }
  1071. File fileSave = getExternalFilesDir(null);
  1072. fileSave = getFilesDir();
  1073. String sourcePath = getExternalFilesDir(null).toString();
  1074. File returnFile = new File(sourcePath + "/" + filename);
  1075. try {
  1076. copyFileStream(returnFile, uri, this);
  1077. Log.d("FILE_EXISTSSS", "copyFileStream");
  1078. } catch (Exception e) {
  1079. e.printStackTrace();
  1080. Log.d("FILE_EXISTSSS", "copyFileStream exception");
  1081. }
  1082. Toast.makeText(this, "Path: " + fileSave.getPath(), Toast.LENGTH_LONG).show();
  1083. return returnFile; //new File(filename);
  1084. //}
  1085. } catch (Exception e) {
  1086. e.printStackTrace();
  1087. }
  1088. return null;
  1089. }
  1090.  
  1091.  
  1092. @Override
  1093. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  1094. super.onActivityResult(requestCode, resultCode, data);
  1095. if (resultCode == RESULT_OK) {
  1096. if (requestCode == SELECT_FILE || requestCode == SELECT_PHOTO) {
  1097. File file = FileManager.getFileByUri(this, data.getData());
  1098. //if (file.exists()) {
  1099. this.file = file;
  1100. this.data = data;
  1101. loadFileOrImagePreview();
  1102.  
  1103.  
  1104.  
  1105. /*} else {
  1106. Toast.makeText(this, getString(R.string.fileupload_could_not_find_file), Toast.LENGTH_LONG).show();
  1107. }*/
  1108. } else if (requestCode == REQUEST_CAMERA) {
  1109. File file = FileManager.getFileByUri(this, data.getData());
  1110. Bitmap bitmap1 = (Bitmap) data.getExtras().get("data");
  1111. cropImage(bitmap1);
  1112. scaleImage(bitmap1, imageWidth, imageHeight);
  1113. ivFile.setImageBitmap(bitmap1);
  1114. displayImage(bitmap1);
  1115. this.file = bitmapToFile(bitmap1, "CameraPhoto.jpg");
  1116.  
  1117.  
  1118. }
  1119. }
  1120. }
  1121.  
  1122.  
  1123. private void loadFileOrImagePreview() {
  1124. if (file != null) {
  1125. String imageRegex = "(.*/)*.+\\.(png|jpg|gif|bmp|jpeg|PNG|JPG|GIF|BMP)$";
  1126. if (file.getName().matches(imageRegex)) {
  1127. Log.d("onActivityResult", "previewImage: " + file.getAbsolutePath());
  1128. Log.d("onActivityResult", "previewImage: " + data.getType());
  1129. previewImage(file, data);
  1130. } else {
  1131. Log.d("onActivityResult", "displayFile");
  1132. displayFile(file);
  1133.  
  1134.  
  1135. }
  1136. }
  1137. }
  1138.  
  1139. private void previewImage(File file, Intent data) {
  1140. Log.d("onActivityResult", "previewImage: ");
  1141. Bitmap bGallery = onSelectFromGalleryResult(data);
  1142. Bitmap cropped = cropImage(bGallery);
  1143. Bitmap scaled = scaleImage(cropped, imageWidth, imageHeight);
  1144. Bitmap bOrientation = fixImageOrientation(file.getPath(), scaled);
  1145. displayImage(bOrientation);
  1146. this.file = bitmapToFile(bOrientation, file.getPath());
  1147.  
  1148. }
  1149.  
  1150. private void copyFileStream(File dest, Uri uri, Context context)
  1151. throws IOException {
  1152. InputStream is = null;
  1153. OutputStream os = null;
  1154. try {
  1155. is = context.getContentResolver().openInputStream(uri);
  1156. os = new FileOutputStream(dest);
  1157. byte[] buffer = new byte[1024];
  1158. int length;
  1159.  
  1160. while ((length = is.read(buffer)) > 0) {
  1161. os.write(buffer, 0, length);
  1162.  
  1163. }
  1164. } catch (Exception e) {
  1165. e.printStackTrace();
  1166. } finally {
  1167. is.close();
  1168. os.close();
  1169. }
  1170. }
  1171.  
  1172.  
  1173. public Bitmap cropImage(Bitmap bitmap) {
  1174. if (bitmap.getWidth() >= bitmap.getHeight()) {
  1175. return Bitmap.createBitmap(bitmap, bitmap.getWidth() / 2 - bitmap.getHeight() / 2, 0, bitmap.getHeight(), bitmap.getHeight());
  1176. } else {
  1177. return Bitmap.createBitmap(bitmap, 0, bitmap.getHeight() / 2 - bitmap.getWidth() / 2, bitmap.getWidth(), bitmap.getWidth());
  1178. }
  1179. }
  1180.  
  1181. public Bitmap scaleImage(Bitmap bitmap, int width, int height) {
  1182. return bitmap;//Bitmap.createScaledBitmap(bitmap, width, height, true);
  1183. }
  1184.  
  1185. public Bitmap fixImageOrientation(String filePath, Bitmap bitmap) {
  1186. try {
  1187. ExifInterface ei = new ExifInterface(filePath);
  1188. int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
  1189.  
  1190. switch (orientation) {
  1191. case ExifInterface.ORIENTATION_ROTATE_90:
  1192. Log.d("ROTATE", "90");
  1193. bitmap = rotateImage(bitmap, 90);
  1194. break;
  1195. case ExifInterface.ORIENTATION_ROTATE_180:
  1196. Log.d("ROTATE", "180");
  1197. bitmap = rotateImage(bitmap, 180);
  1198. break;
  1199. case ExifInterface.ORIENTATION_ROTATE_270:
  1200. Log.d("ROTATE", "270");
  1201. bitmap = rotateImage(bitmap, 270);
  1202. break;
  1203. // etc.
  1204. }
  1205.  
  1206. } catch (IOException ioe) {
  1207. ioe.printStackTrace();
  1208. }
  1209. return bitmap;
  1210. }
  1211.  
  1212. public static Bitmap rotateImage(Bitmap source, float angle) {
  1213. Matrix matrix = new Matrix();
  1214. matrix.postRotate(angle);
  1215. return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
  1216. }
  1217.  
  1218.  
  1219. public void displayImage(final Bitmap bitmap) {
  1220. rlFile.setVisibility(View.VISIBLE);
  1221. rlUploadImage.setVisibility(View.GONE);
  1222. ivFile.setImageBitmap(bitmap);
  1223. ivFile.invalidate();
  1224. }
  1225.  
  1226. public void displayFile(final File file) {
  1227. this.file = file;
  1228.  
  1229.  
  1230. rlFile.setVisibility(View.GONE);
  1231. ivAddFile.setVisibility(View.GONE);
  1232. ivRemoveFile.setVisibility(View.VISIBLE);
  1233. if (file == null) {
  1234. tvFilename.setText("File is null!!");
  1235. } else {
  1236. tvFilename.setText(file.getPath());
  1237. }
  1238. }
  1239.  
  1240. private Bitmap onSelectFromGalleryResult(Intent data) {
  1241. Bitmap bm = null;
  1242. if (data != null) {
  1243. /*
  1244. try {
  1245. //bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
  1246. } catch (IOException e) {
  1247. e.printStackTrace();
  1248. }
  1249. */
  1250. bm = decodeSampledBitmapFromResource(data.getData(), imageWidth, imageHeight); //decodeSampledBitmapFromResource(data.getData())
  1251. }
  1252.  
  1253. return bm;
  1254. }
  1255.  
  1256. // TESTING
  1257. public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
  1258. // Raw height and width of image
  1259. final int height = options.outHeight;
  1260. final int width = options.outWidth;
  1261. int inSampleSize = 1;
  1262.  
  1263. if (height > reqHeight || width > reqWidth) {
  1264.  
  1265. final int halfHeight = height / 2;
  1266. final int halfWidth = width / 2;
  1267.  
  1268. // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  1269. // height and width larger than the requested height and width.
  1270. while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
  1271. inSampleSize *= 2;
  1272. }
  1273. }
  1274.  
  1275. return inSampleSize;
  1276. }
  1277.  
  1278. public Bitmap decodeSampledBitmapFromResource(Uri uri, int reqWidth, int reqHeight) {
  1279. try {
  1280. // First decode with inJustDecodeBounds=true to check dimensions
  1281. final BitmapFactory.Options options = new BitmapFactory.Options();
  1282. options.inJustDecodeBounds = true;
  1283. options.inPreferredConfig = Bitmap.Config.RGB_565;
  1284. options.inDither = true;
  1285. BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
  1286.  
  1287. // Calculate inSampleSize
  1288. options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
  1289.  
  1290. // Decode bitmap with inSampleSize set
  1291. options.inJustDecodeBounds = false;
  1292. return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
  1293. } catch (FileNotFoundException fnfe) {
  1294. fnfe.printStackTrace();
  1295. }
  1296. return null;
  1297. }
  1298. // TESTING
  1299.  
  1300.  
  1301. public String getRealPathFromURI(Uri uri) {
  1302. ///* ALT 1
  1303. Cursor cursor = getContentResolver().query(uri, null, null, null, null);
  1304. cursor.moveToFirst();
  1305. int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
  1306. Log.d("getRealPathFromURI", "idx:" + idx);
  1307. return cursor.getString(idx);
  1308. //*/
  1309.  
  1310. /* ALT 2
  1311. String[] filePathColumn = {MediaStore.Images.Media.DATA};
  1312. Cursor cursor = getContentResolver().query(uri, filePathColumn, null, null, null);
  1313. String yourRealPath = null;
  1314. if(cursor.moveToFirst()){
  1315. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  1316. yourRealPath = cursor.getString(columnIndex);
  1317. } else {
  1318. //boooo, cursor doesn't have rows ...
  1319. }
  1320. cursor.close();
  1321. return yourRealPath;
  1322. */
  1323.  
  1324. }
  1325.  
  1326. public File bitmapToFile(Bitmap bitmap, String filePath) {
  1327. // Upload image
  1328. try {
  1329. Log.d("saveImage", "try 1");
  1330. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  1331. bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
  1332. byte[] bArr = bos.toByteArray();
  1333. bos.flush();
  1334. bos.close();
  1335.  
  1336. String fileName = filePath;
  1337. if (fileName.contains("/")) {
  1338. fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
  1339. }
  1340.  
  1341.  
  1342. FileOutputStream fos = openFileOutput(fileName/*"test_image_name.png"*/, Context.MODE_PRIVATE);
  1343. fos.write(bArr);
  1344. fos.flush();
  1345. fos.close();
  1346. Log.d("FILE_UPLOOOAD", "getFilesDir().getAbsolutePath(): " + getFilesDir().getAbsolutePath());
  1347. Log.d("FILE_UPLOOOAD", "getFilesDir().getName(): " + getFilesDir().getName());
  1348. Log.d("FILE_UPLOOOAD", "getFilesDir().getPath(): " + getFilesDir().getPath());
  1349. File mFile = new File(getFilesDir().getAbsolutePath(), fileName/*"test_image_name.png"*/);
  1350. Log.d("saveImage", "try 2");
  1351. return mFile;
  1352.  
  1353.  
  1354. } catch (IOException ioe) {
  1355. ioe.printStackTrace();
  1356. }
  1357. return null;
  1358. }
  1359.  
  1360. public File uploadFile(String filePath) {
  1361. // Upload image
  1362. try {
  1363.  
  1364. String fileName = filePath;
  1365. if (fileName.contains("/")) {
  1366. fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
  1367. }
  1368.  
  1369.  
  1370. FileOutputStream fos = openFileOutput(fileName/*"test_image_name.png"*/, Context.MODE_WORLD_WRITEABLE);
  1371. fos.write(fileName.getBytes());
  1372. fos.flush();
  1373. fos.close();
  1374. Log.d("FILE_UPLOOOAD", "getFilesDir().getAbsolutePath(): " + getFilesDir().getAbsolutePath());
  1375. Log.d("FILE_UPLOOOAD", "getFilesDir().getName(): " + getFilesDir().getName());
  1376. Log.d("FILE_UPLOOOAD", "getFilesDir().getPath(): " + getFilesDir().getPath());
  1377. File mFile = new File(getFilesDir().getAbsolutePath(), fileName/*"test_image_name.png"*/);
  1378. Log.d("saveImage", "try 2");
  1379. return mFile;
  1380.  
  1381.  
  1382. } catch (IOException ioe) {
  1383. ioe.printStackTrace();
  1384. }
  1385. return null;
  1386. }
  1387.  
  1388.  
  1389. private void sendLoopDialog() {
  1390. String[] tagIds = new String[freeTargetList.size()];
  1391. for (int k = 0; k < selectedTagsArray.size(); k++) {
  1392. tagIds[k] = selectedTagsArray.get(k);
  1393. }
  1394.  
  1395. if (tagIds.length > 0 && selectedTagsArray.size() <= 0) {
  1396.  
  1397. dialogBuilder.setTitle("ERROR");
  1398. dialogBuilder.setMessage("You need to choose at least one tag");
  1399. dialogBuilder.setPositiveButton(getString(R.string.OK), new DialogInterface.OnClickListener() {
  1400. @Override
  1401. public void onClick(DialogInterface dialog, int which) {
  1402. if (loading_progressBar != null) {
  1403. loading_progressBar.setVisibility(View.GONE);
  1404. }
  1405.  
  1406.  
  1407. }
  1408. });
  1409.  
  1410. dialogBuilder.create().show();
  1411. send_loop_button.setEnabled(true);
  1412. pbSendLoop.setVisibility(View.GONE);
  1413. tvSendLoop.setVisibility(View.VISIBLE);
  1414. } else {
  1415. send_loop_button.setEnabled(false);
  1416.  
  1417. AlertDialog.Builder bildah = new AlertDialog.Builder(this);
  1418. bildah.setTitle(getString(R.string._confirm_loop_sending_title));
  1419. bildah.setMessage(getString(R.string._confirm_loop_sending_message));
  1420. bildah.setPositiveButton(getString(R.string._send), new DialogInterface.OnClickListener() {
  1421. @Override
  1422. public void onClick(DialogInterface dialog, int which) {
  1423. sendLoop();
  1424.  
  1425. }
  1426. });
  1427. bildah.setOnCancelListener(new DialogInterface.OnCancelListener() {
  1428. @Override
  1429. public void onCancel(DialogInterface dialog) {
  1430. send_loop_button.setEnabled(true);
  1431. }
  1432. });
  1433. bildah.setNegativeButton(getString(R.string._cancel), new DialogInterface.OnClickListener() {
  1434. @Override
  1435. public void onClick(DialogInterface dialog, int which) {
  1436. send_loop_button.setEnabled(true);
  1437. }
  1438. });
  1439. bildah.create().show();
  1440. }
  1441. }
  1442.  
  1443. private void sendLoop() {
  1444.  
  1445. pbSendLoop.setVisibility(View.VISIBLE);
  1446. tvSendLoop.setVisibility(View.GONE);
  1447.  
  1448. String message = input_message.getText().toString();
  1449. Boolean anonymous = anonymous_checkbox.isChecked();
  1450.  
  1451. //targetArr är id på targeten man har valt, om man då har valt en.
  1452.  
  1453. if (loop_type == INFO_LOOP) {
  1454. File[] files = new File[1];
  1455. files[0] = this.file;
  1456.  
  1457. mWebApiMethods.createNewInfo(group._id, message, files, new JsonHttpResponseHandler() {
  1458. @Override
  1459. public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  1460. super.onSuccess(statusCode, headers, response);
  1461.  
  1462. finishWithResult(RESULT_OK);
  1463. overridePendingTransition(R.anim.nothing, R.anim.right_slide_out);
  1464. send_loop_button.setEnabled(true);
  1465. pbSendLoop.setVisibility(View.GONE);
  1466. tvSendLoop.setVisibility(View.VISIBLE);
  1467. }
  1468.  
  1469. @Override
  1470. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  1471. super.onFailure(statusCode, headers, throwable, errorResponse);
  1472.  
  1473. dialogBuilder.setTitle(getString(R.string.connection_refused));
  1474. dialogBuilder.setMessage(getString(R.string.connection_refused_info));
  1475. dialogBuilder.setPositiveButton(getString(R.string.OK), new DialogInterface.OnClickListener() {
  1476. @Override
  1477. public void onClick(DialogInterface dialog, int which) {
  1478. loading_progressBar.setVisibility(View.GONE);
  1479. }
  1480. });
  1481. dialogBuilder.create().show();
  1482. send_loop_button.setEnabled(true);
  1483. pbSendLoop.setVisibility(View.GONE);
  1484. tvSendLoop.setVisibility(View.VISIBLE);
  1485. }
  1486. });
  1487. } else {
  1488. String targets = null;
  1489. Task task = null;
  1490. String followup = etFollowupAnswer.getText().toString();
  1491. String chosen_target[] = new String[1];
  1492. if (getChosenTask() != null) {
  1493. chosen_target[0] = getChosenTask()._id;
  1494. targets = chosen_target[0];
  1495. task = getChosenTask();
  1496.  
  1497. }
  1498.  
  1499. String[] tagIds = new String[freeTargetList.size()];
  1500. for (int k = 0; k < selectedTagsArray.size(); k++) {
  1501. tagIds[k] = selectedTagsArray.get(k);
  1502. }
  1503.  
  1504. File[] files = new File[1];
  1505. files[0] = this.file;
  1506.  
  1507.  
  1508. mWebApiMethods.createNewReflection(group._id, message, followup, tagIds, selectedFeeling, anonymous, targets, files, new JsonHttpResponseHandler() {
  1509. @Override
  1510. public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
  1511. finishWithResult(RESULT_OK);
  1512. overridePendingTransition(R.anim.nothing, R.anim.right_slide_out);
  1513. super.onSuccess(statusCode, headers, response);
  1514. synchronized (this) {
  1515. notify();
  1516. }
  1517. send_loop_button.setEnabled(true);
  1518. pbSendLoop.setVisibility(View.GONE);
  1519. tvSendLoop.setVisibility(View.VISIBLE);
  1520. }
  1521.  
  1522. @Override
  1523. public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
  1524. super.onFailure(statusCode, headers, throwable, errorResponse);
  1525. dialogBuilder.setTitle(getString(R.string.connection_refused));
  1526. dialogBuilder.setMessage(getString(R.string.connection_refused_info));
  1527. dialogBuilder.setPositiveButton(getString(R.string.OK), new DialogInterface.OnClickListener() {
  1528. @Override
  1529. public void onClick(DialogInterface dialog, int which) {
  1530. if (loading_progressBar != null) {
  1531. loading_progressBar.setVisibility(View.GONE);
  1532. }
  1533. }
  1534. });
  1535. dialogBuilder.create().show();
  1536. send_loop_button.setEnabled(true);
  1537. pbSendLoop.setVisibility(View.GONE);
  1538. tvSendLoop.setVisibility(View.VISIBLE);
  1539. }
  1540. });
  1541.  
  1542. }
  1543. }
  1544.  
  1545.  
  1546. private void finishWithResult(int result) {
  1547. Intent intent = new Intent();
  1548. intent.putExtra("task", task);
  1549. intent.putExtra("group", group);
  1550. setResult(result, intent);
  1551.  
  1552.  
  1553. finish();
  1554. overridePendingTransition(R.anim.nothing, R.anim.right_slide_out);
  1555. }
  1556.  
  1557. private boolean isSendable() {
  1558.  
  1559. if (loop_type == INFO_LOOP) {
  1560.  
  1561. if (this.input_message.getText().toString().trim().length() <= 0) {
  1562.  
  1563. return false;
  1564.  
  1565. }
  1566. } else {
  1567. if (this.input_message.getText().toString().trim().length() <= 0) {
  1568.  
  1569. return false;
  1570. }
  1571.  
  1572. // NEW
  1573. if (isTagsAvailable && selectedTagsArray.isEmpty()) {
  1574.  
  1575. return false;
  1576. }
  1577. // NEW
  1578.  
  1579. /*if (task != null) {
  1580. if (task.followupQuestion != null) {
  1581.  
  1582. Toast.makeText(CreateNewLoop.this,"LOOPINFO", Toast.LENGTH_SHORT).show();
  1583. return etFollowupAnswer.getText().toString().length() > 0;
  1584. }
  1585. }*/
  1586.  
  1587. if (selectedFeeling == -90) {
  1588.  
  1589. return false;
  1590. }
  1591. }
  1592. return true;
  1593.  
  1594. }
  1595.  
  1596. private void enableButtonHelpMethod() {
  1597.  
  1598. if (isSendable()) {
  1599. setAlphaOnSendButton(ENABLED);
  1600. } else {
  1601. setAlphaOnSendButton(DISABLED);
  1602. }
  1603. }
  1604. }
Add Comment
Please, Sign In to add comment