adding a custom service

104 views
Skip to first unread message

Pushp Ranjan

unread,
Mar 15, 2017, 4:56:21 PM3/15/17
to android-platform
I am trying to add a system service in AOSP which when provided a link will download the contents of the link. But when the service is invoked there are some errors. Below are my service implementation and a snapshot of the error. It happens when the service tries to write the file to storage. Is it something related to permissions ? Below are my code and snapshot of error:

/*TestService.java */
package com.android.server;
import android.content.Context;
import android.os.Handler;
import android.os.ITestService;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import android.os.Environment;



public class TestService extends ITestService.Stub {
   
private static final String TAG = "TestService";
   
private TestWorkerThread mWorker;
   
private TestWorkerHandler mHandler;
   
private Context mContext;
   
public TestService(Context context) {
       
super();
        mContext
= context;
        mWorker
= new TestWorkerThread("TestServiceWorker");
        mWorker
.start();
       
Log.i(TAG, "Spawned worker thread");
   
}
 

 
public void setValue(String url) {
     
Log.i(TAG, "set URL " + url);
     
Message msg = Message.obtain();
     msg
.what = TestWorkerHandler.MESSAGE_SET;
     msg
.obj = url;
     mHandler
.sendMessage(msg);
 
}

   
private class TestWorkerThread extends Thread {
       
public TestWorkerThread(String name) {
           
super(name);
       
}
       
public void run() {
           
Looper.prepare();
            mHandler
= new TestWorkerHandler();
           
//mHandler.download();
           
Looper.loop();
       
}
   
}

   
private class TestWorkerHandler extends Handler {
       
private static final int MESSAGE_SET = 0;

       
//String recvUrl ;
       
@Override
       
public void handleMessage(Message msg) {

         
try {
             
if (msg.what == MESSAGE_SET) {
                 
//recvUrl = msg.obj.toString();
                 
Log.i(TAG, "url received: " + msg.obj.toString());

                 
String fileName ;
                 
String receivedUrl = msg.obj.toString();
                 
for (int i = 1; i <= 10; i++) {
                      receivedUrl
= msg.obj.toString();
                      fileName
= "v" + String.valueOf(i) + ".mp4";
                      receivedUrl
+=  fileName;
                     
Log.d(TAG,receivedUrl);

                     
try {
                          URL url
= new URL(receivedUrl);
                         
InputStream input = url.openStream();
                         
Log.d(TAG, input.toString());
                         

                         
/***** ERROR OCCURS HERE ****/
                         
File storagePath = new File(Environment.getExternalStorageDirectory() + "/Movies");
                         
Log.d(TAG," Storage path-"+ storagePath+" filename- "+fileName);
                         
/***** ERROR OCCURS HERE ****/
                         
OutputStream output = new FileOutputStream(new File(storagePath, fileName));
                         
Log.d(TAG, output.toString());
                         
try {
                             
byte[] buffer = new byte[1024];
                             
int bytesRead = 0;
                             
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                                  output
.write(buffer, 0, bytesRead);
                             
}

                         
} finally {
                              output
.close();
                              input
.close();
                         
}
                     
} catch (Exception ex) {
                          ex
.printStackTrace();
                     
}
                 
}
                 
//Log.i(TAG, "url received: " + recvUrl);

               
}
             
}catch (Exception e) {
                 
// Log, don't crash!
                 
Log.e(TAG, "Exception in TestWorkerHandler.handleMessage:", e);
               
}
         
}


       
}

   
}


Screenshot from 2017-03-15 14_11_51.png

biAji

unread,
Mar 15, 2017, 9:49:49 PM3/15/17
to android-platform

Maybe you should use adb  logcat *:e  to get full stack and details of the error.


Pushp Ranjan <pushp...@velankanigroup.com>于2017年3月16日周四 04:56写道:
--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com.
To post to this group, send email to android-...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-platform.
For more options, visit https://groups.google.com/d/optout.

Igor Kalkov

unread,
Mar 15, 2017, 9:49:58 PM3/15/17
to android-platform
Did you check the return value of Environment.getExternalStorageDirectory()? I've seen issues with this method before, when it was used from the system context.

Yafei Liu

unread,
Mar 16, 2017, 10:23:32 AM3/16/17
to android-...@googlegroups.com
The error log has told you: SELinux denied the access of you service, add you service rule to service_contexts in the sepolicy of you device directory (or vender directory).

On Thu, Mar 16, 2017 at 6:34 AM, Igor Kalkov <k4l...@gmail.com> wrote:
Did you check the return value of Environment.getExternalStorageDirectory()? I've seen issues with this method before, when it was used from the system context.

--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platform+unsubscribe@googlegroups.com.
To post to this group, send email to android-platform@googlegroups.com.

AppCoder

unread,
Mar 16, 2017, 2:20:45 PM3/16/17
to android-platform
I don't see where you create the Movies directory (or check that it's already there):

http://stackoverflow.com/questions/6666303/javas-createnewfile-will-it-also-create-directories


Hüseyin Başhan

unread,
Mar 16, 2017, 2:20:56 PM3/16/17
to android-...@googlegroups.com

Did you check SePolicy?

As far as remember, System uid/Platform signed App does not have a permission to write external storage directly.

under the system/sepolicy, look at *.te files

________________________________________
From: android-...@googlegroups.com [android-...@googlegroups.com] on behalf of biAji [biaj...@gmail.com]
Sent: Thursday, March 16, 2017 4:33 AM
To: android-platform
Subject: Re: adding a custom service

Maybe you should use adb logcat *:e to get full stack and details of the error.

Pushp Ranjan <pushp...@velankanigroup.com<mailto:pushp...@velankanigroup.com>>于2017年3月16日周四 04:56写道:
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com<mailto:android-platfo...@googlegroups.com>.
To post to this group, send email to android-...@googlegroups.com<mailto:android-...@googlegroups.com>.
--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-platfo...@googlegroups.com<mailto:android-platfo...@googlegroups.com>.
To post to this group, send email to android-...@googlegroups.com<mailto:android-...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages