1 /*
2  * Copyright 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.keychain;
18 
19 import android.app.Notification;
20 import android.app.PendingIntent;
21 import android.app.Service;
22 import android.content.Intent;
23 import android.os.IBinder;
24 import android.util.Log;
25 
26 public class SecureWebServerService extends Service {
27 
28     // Log tag for this class
29     private static final String TAG = "SecureWebServerService";
30 
31     // A special ID assigned to this on-going notification.
32     private static final int ONGOING_NOTIFICATION = 1248;
33 
34     // A handle to the simple SSL web server
35     private SecureWebServer sws;
36 
37     /**
38      * Start the SSL web server and set an on-going notification
39      */
40     @Override
onCreate()41     public void onCreate() {
42         super.onCreate();
43         sws = new SecureWebServer(this);
44         sws.start();
45         createNotification();
46     }
47 
48     /**
49      * Stop the SSL web server and remove the on-going notification
50      */
51     @Override
onDestroy()52     public void onDestroy() {
53         super.onDestroy();
54         sws.stop();
55         stopForeground(true);
56     }
57 
58     /**
59      * Return null as there is nothing to bind
60      */
61     @Override
onBind(Intent intent)62     public IBinder onBind(Intent intent) {
63         return null;
64     }
65 
66     /**
67      * Create an on-going notification. It will stop the server when the user
68      * clicks on the notification.
69      */
createNotification()70     private void createNotification() {
71         Log.d(TAG, "Create an ongoing notification");
72         Intent notificationIntent = new Intent(this,
73                 KeyChainDemoActivity.class);
74         notificationIntent.putExtra(KeyChainDemoActivity.EXTRA_STOP_SERVER,
75                 true);
76         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
77                 notificationIntent, 0);
78         Notification notification = new Notification.Builder(this).
79                 setContentTitle(getText(R.string.notification_title)).
80                 setContentText(getText(R.string.notification_message)).
81                 setSmallIcon(android.R.drawable.ic_media_play).
82                 setTicker(getText(R.string.ticker_text)).
83                 setOngoing(true).
84                 setContentIntent(pendingIntent).
85                 getNotification();
86         startForeground(ONGOING_NOTIFICATION, notification);
87     }
88 
89 }
90