Skip to content

Commit

Permalink
Corrections on package installations verification
Browse files Browse the repository at this point in the history
The agent cannot be correctly notified of the end of installation
Now a new class make a verification of installations on next cycle and then notify ocsserver
  • Loading branch information
cdpointpoint committed Jan 21, 2015
1 parent 5c134ec commit 3dceddc
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 128 deletions.
19 changes: 14 additions & 5 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.ocsinventoryng.android.agent"
android:installLocation="auto"
android:versionCode="23"
android:versionName="2.1.5" >
android:versionCode="24"
android:versionName="2.1.6c" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:minSdkVersion="9"
android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
Expand Down Expand Up @@ -86,6 +86,15 @@
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="org.ocsinventoryng.android.agent.OCSInstallReceiver"
android:process=":ocs_process" >
<intent-filter android:priority="10000">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.ACTION_PACKAGE_CHANGED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>

</manifest>
</manifest>
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ Revision history for Ocsinventory NG Android Agent
* Package renamed to org.orcinventoryng.android
* Import exported configuration file the name org.orcinventoryng.android.agent_preferences.xml
* OR org.orcinventory.android.agent_preferences.xml
2.1.6
* Bugs correction on package installation managment notifications
* Add InstallReceiver class
2 changes: 1 addition & 1 deletion res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<string name="unit_minutes">minutes</string>
<string name="title_activity_ocslaunch">Installation</string>
<string name="err_cant_connect">Unable to connect to : </string>
<string name="title_activity_ocs__inst">ocs_inst</string>
<string name="title_activity_ocsinst">ocs_inst</string>
<string name="start_download_service">Start package update service</string>

</resources>
115 changes: 99 additions & 16 deletions src/org/ocsinventoryng/android/actions/OCSProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.ocsinventoryng.android.agent.R;

import android.content.Context;
import android.os.AsyncTask;

public class OCSProtocol {
private OCSLog ocslog = OCSLog.getInstance();
Expand Down Expand Up @@ -193,20 +194,23 @@ public String sendmethod(File pFile, String server, boolean gziped) throws OCSPr
}


ocslog.debug("Call : " + server);
HttpResponse localHttpResponse=null;
try {
localHttpResponse = httpClient.execute(httppost);
} catch (ClientProtocolException e) {
throw new OCSProtocolException(e.getMessage());
}
catch (IOException e) {
String msg = appCtx.getString(R.string.err_cant_connect)+" "+e.getMessage();
ocslog.error(msg);
throw new OCSProtocolException(msg);
}
ocslog.debug("Call : " + server);
HttpResponse localHttpResponse=null;
try {
localHttpResponse = httpClient.execute(httppost);
ocslog.debug("Message sent");
} catch (ClientProtocolException e) {
ocslog.error("ClientProtocolException"+e.getMessage());
throw new OCSProtocolException(e.getMessage());
}
catch (IOException e) {
String msg = appCtx.getString(R.string.err_cant_connect)+" "+e.getMessage();
ocslog.error(msg);
throw new OCSProtocolException(msg);
}

try {
try
{
int httpCode = localHttpResponse.getStatusLine().getStatusCode();
ocslog.debug("Response status code : " + String.valueOf(httpCode));
if ( httpCode== 200) {
Expand Down Expand Up @@ -325,15 +329,22 @@ private String extractFreq( String message ) {
return resp;
}

/*
* Function called on main start to verify il a new verion is installed
* Then send success status and delete package
* Operation done in Installereceiver for othet package but INSTALL event is
* not send to then application itself
public void verifyNewVersion(int pvcode) {
File uptdflag = appCtx.getFileStreamPath("update.flag");
String id="";
if ( uptdflag.exists() ) {
try {
String uctx= Utils.readShortFile(uptdflag);
OCSLog.getInstance().debug("uctx :"+uctx);
String[]str = uctx.split(";");
String[]str = uctx.split(":");
if ( str.length > 1 ) {
String id=str[0];
id=str[0];
int vcode=Integer.parseInt(str[1]);
ocslog.debug("Test version code :"+vcode+"="+pvcode);
if ( vcode == pvcode)
Expand All @@ -348,7 +359,79 @@ public void verifyNewVersion(int pvcode) {
}
if ( ! uptdflag.delete() )
ocslog.error("Cant delete update.flag");
// Clean download files
File fapk = new File(appCtx.getExternalCacheDir(),id+".apk");
fapk.delete();
File finst = new File(appCtx.getFilesDir(),appCtx.getPackageName()+".inst");
finst.delete();
File finfo = new File(appCtx.getFilesDir(),id+".info");
finfo.delete();
}
}

*/
/*
* Function called on main start to verify if a new version is installed
* Then send success status and delete package
* Operation done in Installereceiver for othet package but INSTALL event is
* not send to then application itself
*/
public void verifyNewVersion(int pvcode) {
File uptdflag = appCtx.getFileStreamPath("update.flag");
String id="";
OCSLog ocslog=OCSLog.getInstance();
if ( uptdflag.exists() ) {
try {
String uctx= Utils.readShortFile(uptdflag);
OCSLog.getInstance().debug("uctx :"+uctx);
String[]str = uctx.split(":");
if ( str.length > 1 ) {
id=str[0];
int vcode=Integer.parseInt(str[1]);
ocslog.debug("Test version code :"+vcode+"="+pvcode);
if ( vcode == pvcode) {
AsyncSend task = new AsyncSend(appCtx);
task.execute(id, "SUCCESS");
}
// else
// ocsproto.sendRequestMessage("DOWNLOAD", id, "ERR_ABORT");
}
} catch (IOException e) {
ocslog.error("Cant read update.flag");
}
if ( ! uptdflag.delete() )
ocslog.error("Cant delete update.flag");

// Clean download files
File fapk = new File(appCtx.getExternalCacheDir(),id+".apk");
fapk.delete();
File finst = new File(appCtx.getFilesDir(),appCtx.getPackageName()+".inst");
finst.delete();
File finfo = new File(appCtx.getFilesDir(),id+".info");
finfo.delete();
}
}
private class AsyncSend extends AsyncTask<String, Void, Void> {
Context mContext;

AsyncSend(Context ctx) {
mContext = ctx;
}

@Override
protected Void doInBackground(String... params) {
OCSLog ocslog=OCSLog.getInstance();
OCSProtocol ocsproto = new OCSProtocol(mContext);
String ocsid=params[0];
String status=params[1];
try {
ocsproto.sendRequestMessage("DOWNLOAD", ocsid, status);
} catch (OCSProtocolException e) {
ocslog.error(e.getMessage());
}
return null;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class OCSProtocolException extends Exception {
/**
*
*/
private OCSLog ocslog = OCSLog.getInstance();
private static final long serialVersionUID = 8115364599391499226L;

public OCSProtocolException() {
Expand All @@ -15,6 +16,7 @@ public OCSProtocolException() {

public OCSProtocolException(String s) {
super(s);
ocslog.error(s);
}

}
2 changes: 1 addition & 1 deletion src/org/ocsinventoryng/android/agent/OCSAgentActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ private void setStatus(String msg) {
TextView status = (TextView) findViewById(R.id.statusBar);
status.setText(msg);
}

}
31 changes: 0 additions & 31 deletions src/org/ocsinventoryng/android/agent/OCSAgentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,37 +197,6 @@ private void notify(int id) {
}
}

/*
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void notify(int id) {
Notification notif;
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("OCS notification")
.setContentText(getText(id));
Intent rIntent = new Intent(this, OCSAgentActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(OCSAgentActivity.class);
stackBuilder.addNextIntent(rIntent);
PendingIntent intent = PendingIntent.getActivity(this, 0, rIntent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent rpIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(rpIntent);
mNM.notify(id, mBuilder.build());
}
*/


public void onDestroy() {
NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNM.cancelAll();
Expand Down
5 changes: 4 additions & 1 deletion src/org/ocsinventoryng/android/agent/OCSDownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ private int doDownloads() {
for (int iCycle=1; iCycle <= mReply.getPeriode_length() ; iCycle++) {
mOcslog.debug("Period " +iPeriod+" cycle "+iCycle+" todo "+todo);
for ( OCSDownloadIdParams dip : mReply.getIdList() ) {
if ( dip.getInfos() == null )
if ( dip.getInfos() == null ) {
todo --;
continue;
}
int pri=dip.getInfos().getPri();
int m = ( pri == 0 ? 0 : iCycle % pri);
if ( m == 0 ) {
Expand Down Expand Up @@ -169,6 +171,7 @@ private int doDownloads() {
File finst = new File(getExternalCacheDir(),dip.getId()+".apk");
try {
Utils.copyFile(fileOut, finst);
mOcslog.debug("Ready to install : "+finst);
mLaunch=true;
} catch (IOException e) {
mOcslog.error("Erreur : " +e.getMessage());
Expand Down
104 changes: 104 additions & 0 deletions src/org/ocsinventoryng/android/agent/OCSInstallReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.ocsinventoryng.android.agent;

import java.io.File;
import java.io.FileInputStream;

import org.ocsinventoryng.android.actions.OCSLog;
import org.ocsinventoryng.android.actions.OCSProtocol;
import org.ocsinventoryng.android.actions.OCSProtocolException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.widget.Toast;

public class OCSInstallReceiver extends BroadcastReceiver {
private OCSLog mOCSlog;
private String mStatus;
private String mOCSid;

@Override
public void onReceive(final Context ctx, final Intent intent) {
mOCSlog = OCSLog.getInstance();
byte buffer[]= new byte[80];

String data = intent.getData().toString();
mOCSlog.debug("OCSInstallReceiver : "+intent.getAction());
Toast.makeText(ctx, "OCSInstallReceiver : "+intent.getAction(),Toast.LENGTH_SHORT ).show();
String packageName=data.split(":")[1];
mOCSlog.debug("Package : "+packageName);
try {
mOCSlog.debug("Lecture "+packageName+".inst");
FileInputStream fis = ctx.openFileInput(packageName+".inst");
fis.read(buffer);
String s=new String(buffer);
String t[]=s.split(":");
mOCSid=t[0];
int version=Integer.parseInt(t[1]);
fis.close();

if ( isPkgInstalled(ctx,packageName, version )) {
mOCSlog.debug("Package installed return success to OCS server");
mStatus="SUCCESS";
} else {
mOCSlog.debug("Package not installed return fail to OCS server");
mStatus="ERR_ABORTED";
}

AsyncSend task = new AsyncSend(ctx);
task.execute();

// Clean download files
File fapk = new File(ctx.getExternalCacheDir(),mOCSid+".apk");
fapk.delete();
File finst = new File(ctx.getFilesDir(),packageName+".inst");
finst.delete();
File finfo = new File(ctx.getFilesDir(),mOCSid+".info");
finfo.delete();
} catch (Exception e) {
mOCSlog.error(e.getMessage());
}
}
/**
* Check if a package is installed with a given version code
*
* @param pkg Package name
* @param version Version code
* @return true if installed
*/
private boolean isPkgInstalled( Context ctx, String pkg, int version) {
PackageManager pm = ctx.getPackageManager () ;

mOCSlog.debug("Check installation "+pkg+"/"+version);
try {
PackageInfo lpInfo = pm.getPackageInfo (pkg, PackageManager.GET_ACTIVITIES);
return ( lpInfo.versionCode == version );
} catch (NameNotFoundException e) {
mOCSlog.error("Package notfound");
return false;
}
}
private class AsyncSend extends AsyncTask<Void, Void, Void> {
Context mContext;

AsyncSend(Context ctx) {
mContext = ctx;
}

@Override
protected Void doInBackground(Void... params) {
OCSProtocol ocsproto = new OCSProtocol(mContext);
try {
ocsproto.sendRequestMessage("DOWNLOAD", mOCSid, mStatus);
} catch (OCSProtocolException e) {
mOCSlog.error(e.getMessage());
}
return null;
}
}

}
Loading

0 comments on commit 3dceddc

Please sign in to comment.