/*
 * Copyright (C) 2015 Willi Ye
 *
 * 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.grarak.kerneladiutor.services;

import android.content.Intent;

import com.google.android.apps.dashclock.api.DashClockExtension;
import com.google.android.apps.dashclock.api.ExtensionData;
import com.grarak.kerneladiutor.R;
import com.grarak.kerneladiutor.utils.kernel.CPU;
import com.grarak.kerneladiutor.utils.kernel.GPU;
import com.grarak.kerneladiutor.utils.root.RootUtils;

/**
 * Created by willi on 25.03.15.
 */
public class DashClockService extends DashClockExtension {

    private boolean running = false;

    @Override
    protected void onUpdateData(int reason) {
        final String status = getString(R.string.app_name);
        final int cores = CPU.getCoreCount();

        if (!running) new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        String body = "";

                        String cpu = "";
                        for (int i = 0; i < cores; i++) {
                            if (!cpu.isEmpty()) cpu += " | ";
                            int freq = CPU.getCurFreq(i) / 1000;
                            cpu += freq == 0 ? "Offline" : freq;
                        }
                        if (!cpu.isEmpty()) body += "CPU: " + cpu + "\n";
                        body += "GOVERNOR: " + CPU.getCurGovernor(0) + "\n";

                        if (GPU.hasGpuCurFreq())
                            body += "GPU: " + (GPU.getGpuCurFreq() / 1000000) + "MHz";

                        publishUpdate(new ExtensionData()
                                .visible(true)
                                .icon(R.mipmap.ic_launcher)
                                .status(status)
                                .expandedBody(body));
                        Thread.sleep(5000);
                    } catch (Exception e) {
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }).start();
        running = true;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        RootUtils.closeSU();
    }
}

@@ -639,40 +639,8 @@
 s;%0A%0A
-import android.content.Intent;%0A%0A
 impo
@@ -2638,79 +2638,44 @@
 d on
-TaskRemoved(Intent rootIntent) %7B%0A        super.onTaskRemoved(rootIntent
+Destroy() %7B%0A        super.onDestroy(
 );%0A 
@@ -2708,10 +2708,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

close SU correctly