app/src/main/java/org/mcopenplatform/muoapi/managerIapi/ManagerSimService.java
c732d49e
 /*
  *
74ca6d11
  *   Copyright (C) 2020, University of the Basque Country (UPV/EHU)
c732d49e
  *
  *  Contact for licensing options: <licensing-mcpttclient(at)mcopenplatform(dot)com>
  *
  *  This file is part of MCOP MCPTT Client
  *
  *  This is free software: you can redistribute it and/or modify it under the terms of
  *  the GNU General Public License as published by the Free Software Foundation, either version 3
  *  of the License, or (at your option) any later version.
  *
  *  This is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  *  See the GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License along
  *  with this program; if not, write to the Free Software Foundation, Inc.,
  *  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
 package org.mcopenplatform.muoapi.managerIapi;
 
175b478c
 import android.net.Uri;
c732d49e
 import android.os.IBinder;
 import android.os.Message;
 import android.os.RemoteException;
 import android.util.Log;
 
 import org.mcopenplatform.iapi.Constants;
 import org.mcopenplatform.iapi.ISimService;
 import org.mcopenplatform.muoapi.BuildConfig;
 import org.mcopenplatform.muoapi.R;
 
 
 public class ManagerSimService extends ManagerIapiBase{
     private final static String TAG = org.mcopenplatform.muoapi.utils.Utils.getTAG(ManagerSimService.class.getCanonicalName());
 
     //TODO: slotSIM must be defined by capabilities
     protected static int slotSIMCurrent= Constants.Sim.SimSlot.SLOT_ID_2;
     //TODO: SimApp must be defined by capabilities
     //IMP: Tested cards use USIM
     protected static int simAppCurrent= Constants.Sim.SimApp.USIM;
     //TODO: SimAuth must be defined by capabilities
     protected static int simAuthCurrent= Constants.Sim.SimAuth.AKA;
 
     public static final int ERROR_CONFIGURATION=104;
 
175b478c
     protected String PACKET_SERVICE="org.mcopenplatform.iapi.SimService";
     protected String PACKET_MAIN_SERVICE="org.mcopenplatform.iapi";
c732d49e
     private OnSimServiceListener onSimServiceListener;
 
     public ManagerSimService() {
         super();
     }
 
     @Override
175b478c
     protected void isServiceConnected() {
         startConfigurate();
     }
 
     @Override
c732d49e
     protected void startInternal() {
 
     }
 
     @Override
     protected void stopInternal() {
 
     }
 
     @Override
     protected Object registerInterface(IBinder service) {
         ISimService serviceInterface = ISimService.Stub.asInterface(service);
         Log.d(TAG,"Register notification in "+getPACKET_SERVICE());
         try {
             (serviceInterface).registerNotificationReceiver(mcopMessenger);
175b478c
         } catch (RemoteException e) {
             if(BuildConfig.DEBUG)Log.e(TAG,"Error in registerInterface in ManagerSimService:"+e.getMessage());
         }catch (Exception e) {
             if(BuildConfig.DEBUG)Log.e(TAG,"Error in registerInterface in ManagerSimService:"+e.getMessage());
c732d49e
         }
         return serviceInterface;
     }
 
     @Override
     protected boolean receiveEvent(Message message) {
         if(BuildConfig.DEBUG)
             Log.d(TAG,"Execute receiveEvent in "+getPACKET_SERVICE()+": what: "+message.what);
175b478c
         try {
             int error=((ISimService)mService).getErrorCode();
             String errorString=((ISimService)mService).getErrorStr();
             Log.e(TAG,"Error "+getPACKET_SERVICE()+": "+error+" \""+errorString+"\"");
             if(BuildConfig.DEBUG)Log.e(TAG,"Error in ISimService"+" code:"+error+" string:"+errorString);
         }catch (RemoteException e) {
             if(BuildConfig.DEBUG)Log.e(TAG,"Error in getErrorStr in ManagerSimService:"+e.getMessage());
         }catch (Exception e) {
             if(BuildConfig.DEBUG)Log.e(TAG,"Error in getErrorStr in ManagerSimService:"+e.getMessage());
         }
c732d49e
         return false;
     }
 
     /**
      * It allows to distinguish between the different PACKET_SERVICEs for each of the extended class of ManagerIapiBase
      * @return PACKET_SERVICE constant
      */
     @Override
     protected String getPACKET_SERVICE() {
         return PACKET_SERVICE;
     }
 
     @Override
     protected String getPACKET_MAIN_SERVICE() {
         return PACKET_MAIN_SERVICE;
     }
 
175b478c
 
     @Override
     protected void setPACKET_SERVICE(String packetService) {
         changedPacket=true;
         PACKET_SERVICE = packetService;
     }
 
     @Override
     protected void setPACKET_MAIN_SERVICE(String packetMainService) {
         changedPacket=true;
         PACKET_MAIN_SERVICE = packetMainService;
     }
     @Override
     protected boolean checkChangedPacket(){
         return changedPacket;
     }
 
 
 
     protected boolean startConfigurate(){
c732d49e
         if(mService!=null){
             try {
                 //TODO: Not sure if pcscf and impu should be used at all the times
                 String[] pcscfSlot=getPcscf();
                 String[] impu=getImpu();
                 String impi=getImpi();
                 String domain=getDomain();
                 String imei=getImei();
                 String imsi=getImsi();
                 if(BuildConfig.DEBUG){
                     Log.d(TAG,"Configuration client:");
                     if(pcscfSlot!=null && pcscfSlot.length>0) Log.d(TAG,"pcscf: "+pcscfSlot[0]);
                     if(impu!=null && impu.length>0) Log.d(TAG,"impu: "+impu[0]);
                     if(impi!=null) Log.d(TAG,"impi: "+impi);
                     if(domain!=null) Log.d(TAG,"domain: "+domain);
                     if(imei!=null) Log.d(TAG,"imei: "+imei);
                     if(imsi!=null) Log.d(TAG,"imsi: "+imsi);
                 }
175b478c
                 int length=pcscfSlot!=null?pcscfSlot.length:0;
                 String[] pcscfHosts=new String[length];
                 int[] pcscfPorts=new int[length];
 
                 for(int con=0;con<(length);con++){
                     Uri pcscfUri;
                     if(pcscfSlot[con]!=null && (pcscfUri=Uri.parse((pcscfSlot[con].compareToIgnoreCase("sip://")!=0)?("sip://"+pcscfSlot[con]):pcscfSlot[con]))!=null){
                         pcscfHosts[con]=pcscfUri.getHost();
                         pcscfPorts[con]=pcscfUri.getPort();
                     }else{
                         if(BuildConfig.DEBUG)Log.e(TAG,pcscfSlot[con]+ "does not have the correct format");
                     }
                 }
 
                 //TODO: process pcscf to port
                 if(onSimServiceListener!=null)onSimServiceListener.onConfiguration(impu,impi,domain,pcscfHosts,pcscfPorts,imsi,imei);
             }catch (RemoteException e) {
                 if(BuildConfig.DEBUG)Log.e(TAG,"Error in startConfigurate in ManagerSimService:"+e.getMessage());
                 if(onIapiListener!=null)onIapiListener.onIapiError(ERROR_CONFIGURATION,mContext.getString(R.string.Not_be_connected_with_the_service)+getPACKET_SERVICE());
             }catch (Exception e) {
                 if(BuildConfig.DEBUG)Log.e(TAG,"Error in startConfigurate in ManagerSimService:"+e.getMessage());
c732d49e
                 if(onIapiListener!=null)onIapiListener.onIapiError(ERROR_CONFIGURATION,mContext.getString(R.string.Not_be_connected_with_the_service)+getPACKET_SERVICE());
175b478c
 
c732d49e
             }
175b478c
 
c732d49e
         }else{
             if(onIapiListener!=null)onIapiListener.onIapiError(ERROR_CONFIGURATION,mContext.getString(R.string.Error_when_accessing_service)+getPACKET_SERVICE());
             return false;
         }
         return true;
     }
 
     //START METHOD
     private String[] getPcscf() throws RemoteException {
175b478c
         String[] pcscfs=null;
c732d49e
         if(mService==null){
             if(BuildConfig.DEBUG)Log.e(TAG,"getPcscf Error");
             return null;
175b478c
         }else{
             pcscfs=((ISimService)mService).getPcscf(slotSIMCurrent);
             String pcscfString=new String();
             if(pcscfs!=null && pcscfs.length>0)
                 for(String pscrf:pcscfs){
                     pcscfString+="\n"+pscrf;
                 }
             if(BuildConfig.DEBUG)Log.i(TAG,"response getPcscf(): "+pcscfString);
c732d49e
         }
175b478c
         return pcscfs;
c732d49e
     }
 
     private String[] getImpu() throws RemoteException {
175b478c
         String[] impus=null;
c732d49e
         if(mService==null){
             if(BuildConfig.DEBUG)Log.e(TAG,"getImpu Error");
             return null;
175b478c
         }else{
             impus=((ISimService)mService).getImpu(slotSIMCurrent);
             String impusString=new String();
             if(impus!=null && impus.length>0)
             for(String impu:impus){
                 impusString+="\n"+impu;
             }
             if(BuildConfig.DEBUG)Log.i(TAG,"response getImpu(): "+impusString);
c732d49e
         }
175b478c
 
c732d49e
         return ((ISimService)mService).getImpu(slotSIMCurrent);
     }
 
     private String getImsi() throws RemoteException {
175b478c
         String imsi=null;
c732d49e
         if(mService==null){
             if(BuildConfig.DEBUG)Log.e(TAG,"getImsi Error");
             return null;
175b478c
         }else{
             imsi=((ISimService)mService).getSubscriberIdentity(slotSIMCurrent);
             if(BuildConfig.DEBUG)Log.i(TAG,"response getSubscriberIdentity():"+imsi);
c732d49e
         }
175b478c
         return imsi;
c732d49e
     }
 
     private String getImei() throws RemoteException {
175b478c
         String imei=null;
c732d49e
         if(mService==null){
             if(BuildConfig.DEBUG)Log.e(TAG,"getImei Error");
             return null;
175b478c
         }else{
             imei=((ISimService)mService).getDeviceIdentity(slotSIMCurrent);
             if(BuildConfig.DEBUG)Log.i(TAG,"response  getDeviceIdentity(): "+imei);
c732d49e
         }
175b478c
         return imei;
c732d49e
     }
 
     private String getImpi() throws RemoteException {
175b478c
         String impi=null;
c732d49e
         if(mService==null){
             if(BuildConfig.DEBUG)Log.e(TAG,"getImpi Error");
             return null;
         }
175b478c
         else{
             impi=((ISimService)mService).getImpi(slotSIMCurrent);
             if(BuildConfig.DEBUG)Log.i(TAG,"response  getImpi(): "+impi);
         }
         return impi;
c732d49e
     }
 
     private String getDomain() throws RemoteException {
175b478c
         String domain=null;
c732d49e
         if(mService==null){
             if(BuildConfig.DEBUG)Log.e(TAG,"getDomain Error");
             return null;
175b478c
         }else{
             domain=((ISimService)mService).getDomain(slotSIMCurrent);
             if(BuildConfig.DEBUG)Log.i(TAG,"response  getDomain(): "+domain);
c732d49e
         }
175b478c
         return domain;
c732d49e
     }
 
     public String getAuthentication(String data) throws RemoteException {
175b478c
         String response=null;
c732d49e
         if(mService==null){
             if(BuildConfig.DEBUG)Log.e(TAG,"getAuthentication Error");
             return null;
         }else{
             if(BuildConfig.DEBUG)Log.i(TAG,"Executing getAuthentication: "+data);
         }
175b478c
         response=((ISimService)mService).getAuthentication(slotSIMCurrent,simAppCurrent,simAuthCurrent,data);
         if(BuildConfig.DEBUG)Log.i(TAG,"response getAuthentication(): "+response);
         return response;
c732d49e
     }
     //END METHOD
 
175b478c
     public interface OnSimServiceListener{
c732d49e
         void onConfiguration(
175b478c
                 final String[] impu,
                 final String impi,
                 final String domain,
                 final String pcscf[],
                 final int[] pcscfPort,
                 final String imsi,
                 final String imei
c732d49e
         );
     }
 
     public void setOnSimServiceListener(OnSimServiceListener onSimServiceListener){
         this.onSimServiceListener=onSimServiceListener;
     }
 }