Search in sources :

Example 1 with ContributionCalendarDate

use of com.suraj.waext.data.ContributionCalendarDate in project WhatsappExtensions by suraj0208.

the class StatsActivity method getMessagesTimeSpan.

private void getMessagesTimeSpan(final TextView textview, final String jid) {
    (new AsyncTask<Void, Void, Long[]>() {

        @TargetApi(Build.VERSION_CODES.N)
        @Override
        protected Long[] doInBackground(Void... voids) {
            String[] arr = WhatsAppDatabaseHelper.execSQL("/data/data/com.whatsapp/databases/msgstore.db", "select timestamp from messages where key_remote_jid like " + '"' + jid + '"' + " and length(data) > 0 order by timestamp;");
            Calendar c = Calendar.getInstance(Locale.getDefault());
            ArrayList<Integer> medianList = new ArrayList<>();
            TreeMap<ContributionCalendarDate, Integer> currentTreeMap = null;
            String prevMonthYearName = null;
            ContributionCalendarDate prevDate = null;
            if (arr != null) {
                Long[] timestampsFromDatabase = new Long[arr.length];
                for (int i = 0; i < arr.length; i++) {
                    timestampsFromDatabase[i] = Long.parseLong(arr[i]);
                    c.setTime(new Date(timestampsFromDatabase[i]));
                    String monthYearName = new SimpleDateFormat("MMM yyyy").format(c.getTime());
                    ContributionCalendarDate currentDate = new ContributionCalendarDate(timestampsFromDatabase[i]);
                    availableMonths.put(monthYearName, new MonthYearPair(c.get(Calendar.MONTH) + 1, c.get(Calendar.YEAR)));
                    currentTreeMap = dateStringTreeMapHashMap.get(monthYearName);
                    if (currentTreeMap == null) {
                        if (i != 0) {
                            Collections.sort(medianList);
                            int intervalStart = medianList.size() / 3;
                            int intervalEnd = intervalStart + intervalStart;
                            if (medianList.size() > 0 && intervalEnd >= medianList.size() - 1) {
                                dateStringMedianPairHashMap.put(prevMonthYearName, new DistributionPair(medianList.get(intervalStart), medianList.get(medianList.size() - 1), medianList.get(medianList.size() - 1) + 1));
                            } else if (medianList.size() > 0)
                                dateStringMedianPairHashMap.put(prevMonthYearName, new DistributionPair(medianList.get(intervalStart), medianList.get(intervalEnd), medianList.get(medianList.size() - 1)));
                            else {
                                dateStringMedianPairHashMap.put(prevMonthYearName, new DistributionPair(1, 1, 1));
                            }
                            medianList.clear();
                        }
                        currentTreeMap = new TreeMap<>();
                        currentTreeMap.put(currentDate, 1);
                        dateStringTreeMapHashMap.put(monthYearName, currentTreeMap);
                    } else {
                        Integer current = currentTreeMap.get(currentDate);
                        if (current == null) {
                            currentTreeMap.put(currentDate, 1);
                            medianList.add(currentTreeMap.get(prevDate));
                        } else
                            currentTreeMap.put(currentDate, current + 1);
                    }
                    prevMonthYearName = monthYearName;
                    prevDate = currentDate;
                }
                medianList.add(currentTreeMap.get(prevDate));
                Collections.sort(medianList);
                int intervalStart = medianList.size() / 3;
                int intervalEnd = intervalStart + intervalStart;
                if (intervalEnd >= medianList.size() - 1) {
                    dateStringMedianPairHashMap.put(prevMonthYearName, new DistributionPair(medianList.get(intervalStart), medianList.get(medianList.size() - 1), medianList.get(medianList.size() - 1) + 1));
                } else
                    dateStringMedianPairHashMap.put(prevMonthYearName, new DistributionPair(medianList.get(intervalStart), medianList.get(intervalEnd), medianList.get(medianList.size() - 1)));
                ArrayList<HashMap.Entry<String, MonthYearPair>> entries = new ArrayList<>(availableMonths.entrySet());
                Collections.sort(entries, new Comparator<HashMap.Entry<String, MonthYearPair>>() {

                    @Override
                    public int compare(HashMap.Entry<String, MonthYearPair> left, HashMap.Entry<String, MonthYearPair> right) {
                        String thisDate = left.getValue().year + "/" + (Integer.toString(left.getValue().month).length() < 2 ? "0" + left.getValue().month : left.getValue().month);
                        String otherDate = right.getValue().year + "/" + (Integer.toString(right.getValue().month).length() < 2 ? "0" + right.getValue().month : right.getValue().month);
                        return thisDate.compareTo(otherDate);
                    }
                });
                for (Map.Entry<String, MonthYearPair> entry : entries) spinnerData.add(entry.getKey());
                return timestampsFromDatabase;
            }
            return null;
        }

        @TargetApi(Build.VERSION_CODES.N)
        @Override
        protected void onPostExecute(Long[] s) {
            super.onPostExecute(s);
            if (s == null) {
                return;
            }
            if (s.length == 0)
                return;
            String first;
            String second;
            try {
                first = getDateFromTimeStamp(s[0]);
                second = getDateFromTimeStamp(s[s.length - 1]);
                textview.setText(getResources().getString(R.string.messagesTimespan, first, second));
                String monthYearName = new SimpleDateFormat("MMM yyyy").format(s[0]);
                ArrayAdapter<String> adapter = new ArrayAdapter<>(StatsActivity.this, android.R.layout.simple_spinner_dropdown_item, spinnerData);
                spinMonths.setAdapter(adapter);
                spinMonths.setVisibility(View.VISIBLE);
                displayContributionCalendar(monthYearName);
                spinMonths.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        displayContributionCalendar(spinMonths.getItemAtPosition(i).toString());
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {
                    }
                });
                imgViewActivities[0].setBackgroundColor(Color.parseColor(contributionCalendarView.getLowColor()));
                imgViewActivities[1].setBackgroundColor(Color.parseColor(contributionCalendarView.getMediumColor()));
                imgViewActivities[2].setBackgroundColor(Color.parseColor(contributionCalendarView.getHighColor()));
            } catch (NumberFormatException | ArrayIndexOutOfBoundsException ex) {
                ex.printStackTrace();
            }
        }
    }).execute();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Calendar(java.util.Calendar) AsyncTask(android.os.AsyncTask) ImageView(android.widget.ImageView) View(android.view.View) ContributionCalendarView(com.suraj.waext.views.ContributionCalendarView) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) Date(java.util.Date) ContributionCalendarDate(com.suraj.waext.data.ContributionCalendarDate) ContributionCalendarDate(com.suraj.waext.data.ContributionCalendarDate) AdapterView(android.widget.AdapterView) SimpleDateFormat(android.icu.text.SimpleDateFormat) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) ArrayAdapter(android.widget.ArrayAdapter)

Aggregations

SimpleDateFormat (android.icu.text.SimpleDateFormat)1 AsyncTask (android.os.AsyncTask)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ArrayAdapter (android.widget.ArrayAdapter)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 ContributionCalendarDate (com.suraj.waext.data.ContributionCalendarDate)1 ContributionCalendarView (com.suraj.waext.views.ContributionCalendarView)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1