<commit_before>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package org.ado.github.minesync.gui.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;
import org.ado.github.minesync.R;
import org.ado.github.minesync.commons.ALog;
import org.ado.github.minesync.gui.MineSyncMainActivity;
import org.ado.github.minesync.service.MineSyncService;

import static org.ado.github.minesync.config.AppConstants.NOTIFICATION_SYNC;
import static org.apache.commons.lang.Validate.notNull;

/**
 * Notification for the MineSync Service.
 *
 * @author andoni
 * @since 1.0.0
 */
public class MineSyncNotification {

    private static final String TAG = MineSyncNotification.class.getName();
    private final Service service;
    private final NotificationManager notificationManager;
    private Notification notification;
    private RemoteViews notificationTemplate;

    public MineSyncNotification(final Service service) {
        this.service = service;
        notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
        notNull(notificationManager, "notificationManager shouldn't be null");
    }

    public void buildNotification(boolean syncActive) {
        buildNotification(syncActive, true);
    }

    public void buildNotification(boolean syncActive, boolean startForeground) {
        notificationTemplate = new RemoteViews(service.getPackageName(), R.layout.layout_notification);
        PendingIntent pendingIntent = getMainPendingIntent();
        notification = new NotificationCompat.Builder(service.getApplicationContext())
                .setTicker(getResourceText(R.string.label_notification_service_title))
                .setContent(notificationTemplate)
                .setSmallIcon(syncActive ? R.drawable.ic_stat_service : R.drawable.ic_stat_service_disable)
                .setContentIntent(pendingIntent)
//                .setPriority(Notification.PRIORITY_DEFAULT)
                .setOngoing(true)
                .setAutoCancel(false)
                .setColor(service.getResources().getColor(R.color.dropbox_blue))
                .build();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        initActions(syncActive);
/*        if (startForeground) {
            service.startForeground(NOTIFICATION_SYNC, notification);
        }*/
    }

    public void updateSyncState(MineSyncService.SyncStateEnum state) {
        ALog.d(TAG, "updateSyncState - state [" + state + "].");
        if (notificationTemplate != null) {
            switch (state) {
                case UPLOADING_DONWLOADING:
                    notification.icon = R.drawable.ic_stat_upload_download;
                    notificationTemplate.setImageViewResource(R.id.status_icon, R.drawable.ic_stat_upload_download);
                    notificationTemplate.setViewVisibility(R.id.button_service_start, View.INVISIBLE);
                    notificationTemplate.setViewVisibility(R.id.button_service_stop, View.INVISIBLE);
                    notificationTemplate
                            .setTextViewText(R.id.txt_notification_sync_content,
                                    getResourceText(R.string.txt_notification_service_text_active));
                    break;
                case SYNC_ACTIVE:
                    notification.icon = R.drawable.ic_stat_service;
                    notificationTemplate.setImageViewResource(R.id.status_icon, R.drawable.ic_stat_service);
                    notificationTemplate.setViewVisibility(R.id.button_service_start, View.INVISIBLE);
                    notificationTemplate.setViewVisibility(R.id.button_service_stop, View.VISIBLE);
                    notificationTemplate
                            .setTextViewText(R.id.txt_notification_sync_content,
                                    getResourceText(R.string.txt_notification_service_text_active));
                    break;
                case SYNC_DISABLE:
                    notification.icon = R.drawable.ic_stat_service_disable;
                    notificationTemplate.setImageViewResource(R.id.status_icon, R.drawable.ic_stat_service_disable);
                    notificationTemplate.setViewVisibility(R.id.button_service_start, View.VISIBLE);
                    notificationTemplate.setViewVisibility(R.id.button_service_stop, View.INVISIBLE);
                    notificationTemplate
                            .setTextViewText(R.id.txt_notification_sync_content,
                                    getResourceText(R.string.txt_notification_service_text_paused));
                    break;
                case DROPBOX_CONNECTION_ERROR:
                    notification.icon = R.drawable.ic_stat_error;
                    notificationTemplate.setImageViewResource(R.id.status_icon, R.drawable.ic_stat_error);
                    notificationTemplate.setViewVisibility(R.id.button_service_start, View.VISIBLE);
                    notificationTemplate.setViewVisibility(R.id.button_service_stop, View.INVISIBLE);
                    notificationTemplate
                            .setTextViewText(R.id.txt_notification_sync_content,
                                    getResourceText(R.string.txt_notification_dropbox_connection_error));
                    break;
            }
            notificationTemplate.setOnClickPendingIntent(R.id.button_service_start,
                    getSyncAction(MineSyncService.START_SYNC_ACTION));
            notificationTemplate.setOnClickPendingIntent(R.id.button_service_stop,
                    getSyncAction(MineSyncService.STOP_SYNC_ACTION));
        }
        notificationManager.notify(NOTIFICATION_SYNC, notification);
    }

    private void initActions(boolean syncActive) {
        setState(syncActive);
        notificationTemplate.setOnClickPendingIntent(R.id.button_service_start, getSyncAction(MineSyncService.START_SYNC_ACTION));
        notificationTemplate.setOnClickPendingIntent(R.id.button_service_stop, getSyncAction(MineSyncService.STOP_SYNC_ACTION));
    }

    private void setState(boolean syncActive) {<commit_msg>hide icon in top notification bar.<commit_after>/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package org.ado.github.minesync.gui.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;
import org.ado.github.minesync.R;
import org.ado.github.minesync.commons.ALog;
import org.ado.github.minesync.gui.MineSyncMainActivity;
import org.ado.github.minesync.service.MineSyncService;

import static org.ado.github.minesync.config.AppConstants.NOTIFICATION_SYNC;
import static org.apache.commons.lang.Validate.notNull;

/**
 * Notification for the MineSync Service.
 *
 * @author andoni
 * @since 1.0.0
 */
public class MineSyncNotification {

    private static final String TAG = MineSyncNotification.class.getName();
    private final Service service;
    private final NotificationManager notificationManager;
    private Notification notification;
    private RemoteViews notificationTemplate;

    public MineSyncNotification(final Service service) {
        this.service = service;
        notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
        notNull(notificationManager, "notificationManager shouldn't be null");
    }

    public void buildNotification(boolean syncActive) {
        buildNotification(syncActive, true);
    }

    public void buildNotification(boolean syncActive, boolean startForeground) {
        notificationTemplate = new RemoteViews(service.getPackageName(), R.layout.layout_notification);
        PendingIntent pendingIntent = getMainPendingIntent();
        notification = new NotificationCompat.Builder(service.getApplicationContext())
                .setTicker(getResourceText(R.string.label_notification_service_title))
                .setContent(notificationTemplate)
                .setSmallIcon(android.R.color.transparent)
                .setContentIntent(pendingIntent)
//                .setPriority(Notification.PRIORITY_DEFAULT)
                .setOngoing(true)
                .setAutoCancel(false)
                .setColor(service.getResources().getColor(R.color.dropbox_blue))
                .build();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        initActions(syncActive);
/*        if (startForeground) {
            service.startForeground(NOTIFICATION_SYNC, notification);
        }*/
    }

    public void updateSyncState(MineSyncService.SyncStateEnum state) {
        ALog.d(TAG, "updateSyncState - state [" + state + "].");
        if (notificationTemplate != null) {
            switch (state) {
                case UPLOADING_DONWLOADING:
                    notification.icon = R.drawable.ic_stat_upload_download;
                    notificationTemplate.setImageViewResource(R.id.status_icon, R.drawable.ic_stat_upload_download);
                    notificationTemplate.setViewVisibility(R.id.button_service_start, View.INVISIBLE);
                    notificationTemplate.setViewVisibility(R.id.button_service_stop, View.INVISIBLE);
                    notificationTemplate
                            .setTextViewText(R.id.txt_notification_sync_content,
                                    getResourceText(R.string.txt_notification_service_text_active));
                    break;
                case SYNC_ACTIVE:
//                    notification.icon = R.drawable.ic_stat_service;
//                    notificationTemplate.setImageViewResource(R.id.status_icon, R.drawable.ic_stat_service);
                    notificationTemplate.setViewVisibility(R.id.button_service_start, View.INVISIBLE);
                    notificationTemplate.setViewVisibility(R.id.button_service_stop, View.VISIBLE);
                    notificationTemplate
                            .setTextViewText(R.id.txt_notification_sync_content,
                                    getResourceText(R.string.txt_notification_service_text_active));
                    break;
                case SYNC_DISABLE:
//                    notification.icon = R.drawable.ic_stat_service_disable;
//                    notificationTemplate.setImageViewResource(R.id.status_icon, R.drawable.ic_stat_service_disable);
                    notificationTemplate.setViewVisibility(R.id.button_service_start, View.VISIBLE);
                    notificationTemplate.setViewVisibility(R.id.button_service_stop, View.INVISIBLE);
                    notificationTemplate
                            .setTextViewText(R.id.txt_notification_sync_content,
                                    getResourceText(R.string.txt_notification_service_text_paused));
                    break;
                case DROPBOX_CONNECTION_ERROR:
                    notification.icon = R.drawable.ic_stat_error;
                    notificationTemplate.setImageViewResource(R.id.status_icon, R.drawable.ic_stat_error);
                    notificationTemplate.setViewVisibility(R.id.button_service_start, View.VISIBLE);
                    notificationTemplate.setViewVisibility(R.id.button_service_stop, View.INVISIBLE);
                    notificationTemplate
                            .setTextViewText(R.id.txt_notification_sync_content,
                                    getResourceText(R.string.txt_notification_dropbox_connection_error));
                    break;
            }
            notificationTemplate.setOnClickPendingIntent(R.id.button_service_start,
                    getSyncAction(MineSyncService.START_SYNC_ACTION));
            notificationTemplate.setOnClickPendingIntent(R.id.button_service_stop,
                    getSyncAction(MineSyncService.STOP_SYNC_ACTION));
        }
        notificationManager.notify(NOTIFICATION_SYNC, notification);
    }

    private void initActions(boolean syncActive) {
        setState(syncActive);
        notificationTemplate.setOnClickPendingIntent(R.id.button_service_start, getSyncAction(MineSyncService.START_SYNC_ACTION));
        notificationTemplate.setOnClickPendingIntent(R.id.button_service_stop, getSyncAction(MineSyncService.STOP_SYNC_ACTION));
    }

    private void setState(boolean syncActive) {
{'fraction_non_alphanumeric': 0.07397480568212275, 'fraction_numerical': 0.0010720986330742428, 'mean_word_length': 2.826923076923077, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5111698', 'n_tokens_mistral': 3796, 'n_tokens_neox': 3631, 'n_words': 765}