<commit_before>/*
 * Copyright (C) 2015 The Nevolution Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.oasisfeng.nevo.decorators.whatsapp;

import android.os.RemoteException;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;

import com.oasisfeng.android.os.IBundle;
import com.oasisfeng.nevo.INotification;
import com.oasisfeng.nevo.StatusBarNotificationEvo;
import com.oasisfeng.nevo.decorator.NevoDecoratorService;

import java.util.ArrayList;
import java.util.List;

import static android.graphics.Typeface.BOLD;
import static android.support.v4.app.NotificationCompat.EXTRA_SUMMARY_TEXT;
import static android.support.v4.app.NotificationCompat.EXTRA_TEXT;
import static android.support.v4.app.NotificationCompat.EXTRA_TEXT_LINES;
import static android.support.v4.app.NotificationCompat.EXTRA_TITLE;
import static android.support.v4.app.NotificationCompat.EXTRA_TITLE_BIG;
import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;

/**
 * App-specific decorator for WhatsApp
 *
 * Created by Oasis on 2016/3/1.
 */
public class WhatsAppDecorator extends NevoDecoratorService {

	@Override public void apply(final StatusBarNotificationEvo evolving) throws RemoteException {
		final INotification n = evolving.notification();
		final IBundle extras = n.extras();

		final CharSequence who, group, message;
		@SuppressWarnings("unchecked") final List<CharSequence> lines = extras.getCharSequenceArray(EXTRA_TEXT_LINES);
		final boolean has_lines = lines != null && ! lines.isEmpty();
		final CharSequence title = extras.getCharSequence(EXTRA_TITLE);
		final CharSequence last = has_lines ? lines.get(lines.size() - 1) : extras.getCharSequence(EXTRA_TEXT);

		final CharSequence[] last_parts = extract(title, last);
		who = last_parts[0]; group = last_parts[1]; message = last_parts[2];

		if (group != null) {
			evolving.setId(group.toString().hashCode());
			evolving.setTag(".Group");
		} else if (who != null) {
			evolving.setId(who.toString().hashCode());
			evolving.setTag(".Direct");
		} else return;	// Nothing to do for other messages.

		final CharSequence new_title = group != null ? group : who;
		evolving.setId(new_title.toString().hashCode());

		if (! has_lines) return;

		extras.putCharSequence(EXTRA_TITLE, new_title);
		extras.putCharSequence(EXTRA_TITLE_BIG, new_title);
		extras.putCharSequence(EXTRA_TEXT, group != null ? who + ": " + message : message);

		final List<CharSequence> new_lines = new ArrayList<>(lines.size());
		for (final CharSequence line : lines) {
			final CharSequence[] parts = extract(title, line);
			if (group != null) {			// Group chat, keep messages within the same group.
				if (! group.equals(parts[1])) continue;
				final SpannableStringBuilder new_line = new SpannableStringBuilder();
				new_line.append(parts[0]).setSpan(new StyleSpan(BOLD), 0, new_line.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
				new_lines.add(new_line.append(": ").append(parts[2]));
			} else if (who.equals(parts[0]) && parts[1] == null)
				new_lines.add(parts[2]);	// Direct chat, keep messages from the same person (excluding group chat)
		}
		extras.putCharSequenceArray(EXTRA_TEXT_LINES, new_lines);
		extras.remove(EXTRA_SUMMARY_TEXT);
		extras.putString(EXTRA_REBUILD_STYLE, STYLE_INBOX);
		if (new_lines.size() > 1) n.setNumber(new_lines.size());
	}

	/**
	 * Patterns
	 *
	 * 		Title			Line
	 * 		-----			----<commit_msg>Minor tweaks to WhatsAppDecorator.<commit_after>/*
 * Copyright (C) 2015 The Nevolution Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.oasisfeng.nevo.decorators.whatsapp;

import android.app.Notification;
import android.os.Build;
import android.os.RemoteException;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;

import com.oasisfeng.android.os.IBundle;
import com.oasisfeng.nevo.INotification;
import com.oasisfeng.nevo.StatusBarNotificationEvo;
import com.oasisfeng.nevo.decorator.NevoDecoratorService;

import java.util.ArrayList;
import java.util.List;

import static android.graphics.Typeface.BOLD;
import static android.support.v4.app.NotificationCompat.EXTRA_SUMMARY_TEXT;
import static android.support.v4.app.NotificationCompat.EXTRA_TEXT;
import static android.support.v4.app.NotificationCompat.EXTRA_TEXT_LINES;
import static android.support.v4.app.NotificationCompat.EXTRA_TITLE;
import static android.support.v4.app.NotificationCompat.EXTRA_TITLE_BIG;
import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;

/**
 * App-specific decorator for WhatsApp
 *
 * Created by Oasis on 2016/3/1.
 */
public class WhatsAppDecorator extends NevoDecoratorService {

	private static final int DEFAULT_COLOR = 0xFF075E54;

	@Override public void apply(final StatusBarNotificationEvo evolving) throws RemoteException {
		final INotification n = evolving.notification();
		final IBundle extras = n.extras();

		final CharSequence who, group, message;
		@SuppressWarnings("unchecked") final List<CharSequence> lines = extras.getCharSequenceArray(EXTRA_TEXT_LINES);
		final boolean has_lines = lines != null && ! lines.isEmpty();
		final CharSequence title = extras.getCharSequence(EXTRA_TITLE);
		final CharSequence last = has_lines ? lines.get(lines.size() - 1) : extras.getCharSequence(EXTRA_TEXT);

		final CharSequence[] last_parts = extract(title, last);
		who = last_parts[0]; group = last_parts[1]; message = last_parts[2];

		if (group != null) evolving.setTag(".Group");
		else if (who != null) evolving.setTag(".Direct");
		else return;	// Nothing to do for other messages.

		final CharSequence new_title = group != null ? group : who;
		evolving.setId(new_title.toString().hashCode());
		if (n.getColor() == 0) n.setColor(DEFAULT_COLOR);	// Fix the missing color in some notifications

		if (! has_lines) return;

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
			n.removeFlags(Notification.FLAG_GROUP_SUMMARY);
		else extras.remove("android.support.isGroupSummary");

		extras.putCharSequence(EXTRA_TITLE, new_title);
		extras.putCharSequence(EXTRA_TITLE_BIG, new_title);
		extras.putCharSequence(EXTRA_TEXT, group != null ? who + ": " + message : message);

		final List<CharSequence> new_lines = new ArrayList<>(lines.size());
		for (final CharSequence line : lines) {
			final CharSequence[] parts = extract(title, line);
			if (group != null) {			// Group chat, keep messages within the same group.
				if (! group.equals(parts[1])) continue;
				final SpannableStringBuilder new_line = new SpannableStringBuilder();
				new_line.append(parts[0]).setSpan(new StyleSpan(BOLD), 0, new_line.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
				new_lines.add(new_line.append(": ").append(parts[2]));
			} else if (who.equals(parts[0]) && parts[1] == null)
				new_lines.add(parts[2]);	// Direct chat, keep messages from the same person (excluding group chat)
		}
		extras.putCharSequenceArray(EXTRA_TEXT_LINES, new_lines);
		extras.remove(EXTRA_SUMMARY_TEXT);
		extras.putString(EXTRA_REBUILD_STYLE, STYLE_INBOX);
		if (new_lines.size() > 1) n.setNumber(new_lines.size());
	}

	/**
	 * Patterns
	 *
	 * 		Title			Line
	 * 		-----			----
{'fraction_non_alphanumeric': 0.11344125582536178, 'fraction_numerical': 0.008462104488594555, 'mean_word_length': 4.905141202027516, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6004811', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2524, 'n_words': 636}