aboutsummaryrefslogtreecommitdiffstats
path: root/ccnxandroidmetis/MetisControl/src/main/java/com/metis/ccnx/ccnxsdk/metiscontrol/MetisService.java
blob: ee5ef5be30d850e2ee668179656a65d38dc525e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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.metis.ccnx.ccnxsdk.metiscontrol;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.metis.ccnx.ccnxsupportlibrary.Metis;
import android.widget.TextView;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class MetisService extends Service {
    private final static String TAG = "CCNx.MetisService";

    private static Thread sForwarderThread = null;

    public MetisService() {
    }

    private String path;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Metis metis = Metis.getInstance();
        if (!metis.isRunning()) {
            Log.d(TAG, "Starting Metis");
            String path = null;
            if (intent != null &&  intent.getExtras() != null &&  intent.getExtras().get("path") != null) {

                path = intent.getExtras().get("path").toString();
                startForwarder(intent, path);
            } else {
                //TextView mPathTextView = (TextView) view.findViewById(R.id.pathText);
                startForwarder(intent, "/storage/emulated/0/MetisConf/metis.cfg".toString());
            }
        } else {
            Log.d(TAG, "Metis already running.");
        }
        // Tell Android we want it restarted if it dies or is killed by the OS.
        return Service.START_STICKY;
    }


    @Override
    public void onDestroy() {
        //get Metis instance
        Metis metis = Metis.getInstance();
        Log.d(TAG, "Destroying");
        if (metis.isRunning()) {
            Log.d(TAG, "Trying to stop Metis: " + metis.toString());
            metis.stop();
            stopForeground(true);
        }
        super.onDestroy();
    }

    protected Runnable mForwarderRunner = new Runnable() {

        //private String path;
        @Override
        public void run() {
            Metis metis = Metis.getInstance();

            metis.start(path);
        }


    };


    private void startForwarder(Intent intent, String path) {

        int NOTIFICATION_ID = 12345;

        Metis metis = Metis.getInstance();
        if (!metis.isRunning()) {
            this.path = path;
            sForwarderThread = new Thread(mForwarderRunner, "CCNx.MetisRunner");
            sForwarderThread.start();
            metis.isRunning();
            Intent resultIntent = new Intent(this, ForwarderStatusActivity.class);

            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

            Notification notification = new Notification.Builder(getApplicationContext())
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText("Metis is running")
                    // .setSmallIcon(R.drawable.ic_notification)
                    .setSmallIcon(R.mipmap.ic_notification)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .build();

            notification.flags |= Notification.FLAG_NO_CLEAR;

            startForeground(NOTIFICATION_ID, notification);
        }
    }


}