aboutsummaryrefslogtreecommitdiffstats
path: root/MetisForwarderAndroid/app/src/main/java/icn/forwarder/com/service/ForwarderAndroidService.java
blob: 95668a0877551cb35fc574f1d6fdc3be72996984 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (c) 2018 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 icn.forwarder.com.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.EditText;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import icn.forwarder.com.forwarderandroid.ForwarderAndroidActivity;
import icn.forwarder.com.supportlibrary.Forwarder;
import icn.forwarder.com.utility.Constants;
import icn.forwarder.com.utility.ResourcesEnumerator;

public class ForwarderAndroidService extends Service {
    private final static String TAG = "ForwarderService";

    private static Thread sForwarderThread = null;
    private EditText configurationEditText;

    public ForwarderAndroidService() {
    }

    private String path;

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


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

        Forwarder forwarder = Forwarder.getInstance();
        if (!forwarder.isRunning()) {
            Log.d(TAG, "Starting Forwarder");
            SharedPreferences sharedPreferences = getSharedPreferences(Constants.FORWARDER_PREFERENCES, MODE_PRIVATE);
            String configuration = sharedPreferences.getString(ResourcesEnumerator.CONFIGURATION.key(), Constants.DEFAULT_CONFIGURATION);
            String sourceIp = sharedPreferences.getString(ResourcesEnumerator.SOURCE_IP.key(), null);
            String sourcePort = sharedPreferences.getString(ResourcesEnumerator.SOURCE_PORT.key(), null);
            String nextHopIp = sharedPreferences.getString(ResourcesEnumerator.NEXT_HOP_IP.key(), null);
            String nextHopPort = sharedPreferences.getString(ResourcesEnumerator.NEXT_HOP_PORT.key(), null);
            String prefix = sharedPreferences.getString(ResourcesEnumerator.PREFIX.key(), null);
            configuration = configuration.replace(Constants.SOURCE_IP, sourceIp);
            configuration = configuration.replace(Constants.SOURCE_PORT, sourcePort);
            configuration = configuration.replace(Constants.NEXT_HOP_IP, nextHopIp);
            configuration = configuration.replace(Constants.NEXT_HOP_PORT, nextHopPort);
            configuration = configuration.replace(Constants.PREFIX, prefix);
            try {
                String configurationDir = getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir +
                        File.separator + Constants.CONFIGURATION_PATH;
                File folder = new File(configurationDir);
                if (!folder.exists()) {
                    folder.mkdirs();
                }

                writeToFile(configuration, configurationDir + File.separator + Constants.CONFIGURATION_FILE_NAME);
                Log.d(TAG, configurationDir + File.separator + Constants.CONFIGURATION_FILE_NAME);
                startForwarder(intent, configurationDir + File.separator + Constants.CONFIGURATION_FILE_NAME);
            } catch (PackageManager.NameNotFoundException e) {
                Log.w(TAG, "Error Package name not found ", e);
            }


        } else {
            Log.d(TAG, "Forwarder already running.");
        }
        return Service.START_STICKY;
    }


    @Override
    public void onDestroy() {
        Forwarder forwarder = Forwarder.getInstance();
        Log.d(TAG, "Destroying Forwarder");
        if (forwarder.isRunning()) {
            forwarder.stop();
            stopForeground(true);
        }
        super.onDestroy();
    }

    protected Runnable mForwarderRunner = new Runnable() {

        //private String path;
        @Override
        public void run() {
            Forwarder forwarder = Forwarder.getInstance();
            forwarder.start(path);
        }


    };

    private boolean writeToFile(String data, String path) {
        Log.v(TAG, path + " " + data);
        try (Writer writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(path), "utf-8"))) {
            writer.write(data);
            return true;
        } catch (IOException e) {
            Log.e(TAG, "File write failed: " + e.toString());
            return false;
        }
    }


    private void startForwarder(Intent intent, String path) {
        String NOTIFICATION_CHANNEL_ID = "12345";
        Notification.Builder notificationBuilder = null;
        if (Build.VERSION.SDK_INT >= 26) {
            notificationBuilder =
                    new Notification.Builder(this, NOTIFICATION_CHANNEL_ID);
        } else {
            notificationBuilder = new Notification.Builder(this);
        }

        Intent notificationIntent = new Intent(this, ForwarderAndroidActivity.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        notificationBuilder.setContentTitle("ForwarderAndroid").setContentText("ForwarderAndroid").setOngoing(true).setContentIntent(activity);
        Notification notification = notificationBuilder.build();

        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "ForwarderAndroid", NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription("ForwarderAndroid");
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);

        }

        startForeground(Constants.FOREGROUND_SERVICE, notification);


        Forwarder forwarder = Forwarder.getInstance();
        if (!forwarder.isRunning()) {
            this.path = path;
            sForwarderThread = new Thread(mForwarderRunner, "ForwarderRunner");
            sForwarderThread.start();
        }


        Log.e(TAG, "ForwarderAndroid starterd");

    }
}