1. 程式人生 > >電話錄音按號碼或者聯絡人建立資料夾儲存

電話錄音按號碼或者聯絡人建立資料夾儲存

一、分析
系統錄音功能實現方法:

在packages\services\Telecomm\src\com\mediatek\telecom\recording\Recorder.java
   public void startRecording(int outputfileformat, String extension) throws IOException {
       log("startRecording");
       stop();
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
       String prefix = dateFormat.format(new Date());
       File sampleDir = new File(StorageManagerEx.getDefaultPath());
       if (!sampleDir.canWrite()) {
           Log.i(TAG, "----- file can't write!! ---");
           // Workaround for broken sdcard support on the device.
           sampleDir = new File("/sdcard/sdcard");
       }
       sampleDir = new File(sampleDir.getAbsolutePath() + "/PhoneRecord");
       if (!sampleDir.exists()) {
           sampleDir.mkdirs();
       }
       /// For ALPS01000670. @{
       // get the current path where saved recording files.
       mRecordStoragePath = sampleDir.getCanonicalPath();
       /// @}
       try {
           mSampleFile = File.createTempFile(prefix, extension, sampleDir);
       } catch (IOException e) {
           setError(SDCARD_ACCESS_ERROR);
           Log.i(TAG, "----***------- can't access sdcard !! " + e);
           throw e;
       }
       log("finish creating temp file, start to record");
       ..........
   }
在這個方法中
     sampleDir = new File(sampleDir.getAbsolutePath() + "/PhoneRecord");
    if (!sampleDir.exists()) {
          sampleDir.mkdirs();
    }

即建立檔案的關鍵,sampleDir.getAbsolutePath() + “/PhoneRecord” 即為建立的檔案的路徑,所以只要在這裡修改為:
sampleDir.getAbsolutePath() + “/PhoneRecord” + callinfo;//callinfo即為電話號碼(聯絡人)。
所以問題的關鍵轉化為在public void startRecording(int outputfileformat, String extension)方法中增加一個電話資訊的引數
public void startRecording(int outputfileformat, String extension ,String callinfo);
問題分析到這裡,就簡單了,方便起見,我們增加新的介面,按照電話錄音的邏輯流程,把callinfo傳遞過來

二、實現
1.frameworks\base\telecomm\java\com\android\internal\telecom\IInCallAdapter.aidl
增加介面:引數 String callinfo傳遞的為電話號碼(聯絡人)
//add start
void startVoiceRecordings(String callinfo);
//add end

2.frameworks\base\telecomm\java\android\telecom\InCallAdapter.java
增加方法:

  //add   start
  public void startVoiceRecordings(String callinfo){
    try {
   	Log.i("YH","yh-----callinfo:"+callinfo);
       mAdapter.startVoiceRecordings(callinfo);
    } catch (RemoteException ignored) {
    }
  }
  //add  end

3.frameworks\base\telecomm\java\android\telecom\Phone.java
增加方法:

  //add  start
  public final void startVoiceRecordings(String callinfo) {
   	 Log.wtf(this, "yh-----Phone %s :",callinfo);
       mInCallAdapter.startVoiceRecordings(callinfo);
   }
 //add  end

4.packages\apps\InCallUI\src\com\android\incallui\CallButtonPresenter.java
(1).修改public void voiceRecordClicked()方法為:

    public void voiceRecordClicked() {
       //TelecomAdapter.getInstance().startVoiceRecording();
       if("1".equals(android.os.SystemProperties.get("ro.wind.itx.call"))){
          String callInfo= getNameorNumber();
          Log.d(this, "yh-----callbutton  callInfo" + callInfo);  	  	
          TelecomAdapter.getInstance().startVoiceRecordings(callInfo);
   	}else{
   	  TelecomAdapter.getInstance().startVoiceRecording();
       }
   }

(2).增加方法:

 String getNameorNumber(){
          String nameOrNumber =null;
          ContactInfoCache  mContactInfoCache = null;
          ContactInfoCache.ContactCacheEntry mContactCatcheEntry = null;
          if(mCall !=null){
             mContactInfoCache = ContactInfoCache.getInstance(InCallPresenter.getInstance().getContext());
             if(mContactInfoCache !=null){
               mContactCatcheEntry = mContactInfoCache.getInfo(mCall.getId());
             }
             if(mContactCatcheEntry !=null){
                if(mContactCatcheEntry.name !=null){
                   nameOrNumber = mContactCatcheEntry.name;
                   Log.d(this, "yh-----call  mContactCatcheEntry.name" + mContactCatcheEntry.name);
                }else if(mContactCatcheEntry.number !=null){
                   nameOrNumber = mContactCatcheEntry.number;
                   Log.d(this, "yh-----call  mContactCatcheEntry.number" +mContactCatcheEntry.number);
                }
             }
         }
    return nameOrNumber;

}

5.packages\apps\InCallUI\src\com\android\incallui\TelecomAdapter.java
增加方法:

    //add   start
   void startVoiceRecordings(String callInfo) {
       if (mPhone != null) {
           Log.i(this, "yh---TelecomAdapter callInfo:"+callInfo);
           mPhone.startVoiceRecordings(callInfo);
       } else {
           Log.e(this, "error startVoiceRecording, mPhone is null");
       }
   }
   //add  end

6.packages\services\Telecomm\src\com\android\server\telecom\CallsManager.java 增加方法:

   //add  start
   void startVoiceRecordings(String callinfo) {
       Log.v(this, "yh----callsmanager:callinfo:"+callinfo);	
       PhoneRecorderHandler.getInstance().startVoiceRecords(
       PhoneRecorderHandler.PHONE_RECORDING_VOICE_CALL_CUSTOM_VALUE,callinfo);
   }
   //add  end

7.packages\services\Telecomm\src\com\android\server\telecom\InCallAdapter.java

修改public void handleMessage(Message msg)方法:
        @Override
       public void handleMessage(Message msg) {
           Call call;
           switch (msg.what) {
                ......		
                case MSG_START_VOICE_RECORDING:
                //add   start
                //mCallsManager.startVoiceRecording();
                if("1".equals(android.os.SystemProperties.get("ro.wind.itx.call"))){
                    String callinfo =(String) msg.obj;
                    Log.w(this, "yh--MSG_START_VOICE_RECORDING: %s", msg.obj);
                    Log.w(this, "yh---callinfo"+callinfo);
                    mCallsManager.startVoiceRecordings(callinfo);
                }else{
                    mCallsManager.startVoiceRecording();
                }
                //add  end     
                break;
                .......
             }
         }
增加方法:
  //add  start
  @Override
   public void startVoiceRecordings(String callinfo) {
       mHandler.obtainMessage(MSG_START_VOICE_RECORDING,callinfo).sendToTarget();
   }
  //add  end

8.packages\services\Telecomm\src\com\mediatek\telecom\recording\IPhoneRecorder.aidl 增加介面:

//add  start
   void startRecords(String callinfo);
//add  end

9.packages\services\Telecomm\src\com\mediatek\telecom\recording\PhoneRecorder.java 增加方法:

    //add   start
   public void startRecords(String callinfo) {
       log("startRecord, mRequestedType = " + mRequestedType);
       if (sIsRecording) {
           return;
       }
       if (!RecorderUtils.isExternalStorageMounted(mContext)) {
           mSampleInterrupted = true;
           Log.e(TAG, "-----Please insert an SD card----");
       } else if (!RecorderUtils.diskSpaceAvailable(
           PhoneRecorderHandler.PHONE_RECORD_LOW_STORAGE_THRESHOLD)) {
           mSampleInterrupted = true;
           Log.e(TAG, "--------Storage is full-------");
       } else {
           Log.e(TAG, "yh-----startRecords ----callinfo:"+callinfo);
           try {
               if (AUDIO_AMR.equals(mRequestedType)) {
                   startRecordings(MediaRecorder.OutputFormat.RAW_AMR, ".amr",callinfo);
               } else if (AUDIO_3GPP.equals(mRequestedType)) {
                   startRecordings(MediaRecorder.OutputFormat.THREE_GPP, ".3gpp",callinfo);
               } else if (AUDIO_ANY.equals(mRequestedType)) {
                   // mRequestedType = AUDIO_3GPP;
                   startRecordings(MediaRecorder.OutputFormat.THREE_GPP, ".3gpp",callinfo);
               } else {
                   throw new IllegalArgumentException("Invalid output file type requested");
               }
               sIsRecording = true; // start successfully.qintx
           } catch (IOException oe) {
               sIsRecording = false;
           }
       }
   }
  //add  end

10.packages\services\Telecomm\src\com\mediatek\telecom\recording\PhoneRecorderHandler.java

修改:private ServiceConnection mConnection = new ServiceConnection():
     private ServiceConnection mConnection = new ServiceConnection() {
       public void onServiceConnected(ComponentName className, IBinder service) {
           mPhoneRecorder = IPhoneRecorder.Stub.asInterface(service);
           try {
               log("onServiceConnected");
               if (null != mPhoneRecorder) {
                   mPhoneRecorder.listen(mPhoneRecordStateListener);
                   //add  start
                  // mPhoneRecorder.startRecord();
                  if("1".equals(android.os.SystemProperties.get("ro.wind.itx.call"))){
                     Log.d(LOG_TAG,"yh-----onServiceConnected:"+mCallInfo);
                     mPhoneRecorder.startRecords(mCallInfo);			
                   }else{
                     mPhoneRecorder.startRecord();
                   }
                   //add  end
                   mHandler.postDelayed(mRecordDiskCheck, 500);
               }
           } catch (RemoteException e) {
              Log.e(LOG_TAG, "onServiceConnected: couldn't register to record service",
                       new IllegalStateException());
           }
       }
增加方法:
   //add  start
   private String mCallInfo;
   public void startVoiceRecords(final int customValue ,String callinfo) {
       mCustomValue = customValue;
       mRecordType = PhoneRecorderHandler.PHONE_RECORDING_TYPE_ONLY_VOICE;
       mRecordStoragePath = RecorderUtils.getExternalStorageDefaultPath();
       mPhoneRecorderState = PhoneRecorder.RECORDING_STATE;
       mCallInfo = callinfo;
       if (null != mRecorderServiceIntent && null == mPhoneRecorder) {
           Log.d(LOG_TAG, "yh----null startVoiceRecords:"+mCallInfo);
           TelecomGlobals.getInstance().getContext().bindService(mRecorderServiceIntent, mConnection,
                   Context.BIND_AUTO_CREATE);
       } else if (null != mRecorderServiceIntent && null != mPhoneRecorder) {
           try {
               Log.d(LOG_TAG, "yh----startVoiceRecords:"+callinfo);
               mPhoneRecorder.startRecords(callinfo);
               mHandler.postDelayed(mRecordDiskCheck, 500);
           } catch (RemoteException e) {
               Log.e(LOG_TAG, "start Record failed", new IllegalStateException());
           }
       }
   }
  //add  end

11.packages\services\Telecomm\src\com\mediatek\telecom\recording\PhoneRecorderServices.java

修改public void handleMessage(Message msg)方法:
        @Override
       public void handleMessage(Message msg) {
           ......
           case REQUEST_START_RECORDING:
            if (null != mPhoneRecorder) {
                log("[handleMessage]do start recording");
                //add   start
                // mPhoneRecorder.startRecord();
                if("1".equals(android.os.SystemProperties.get("ro.wind.itx.call"))){
                   String callinfo = (String)msg.obj;
                   Log.d("YH","yh-----handleMessage callinfo:"+callinfo);
                   mPhoneRecorder.startRecords(callinfo);
                }else{
                   mPhoneRecorder.startRecord();
                }
               //add   end
               }
           break;
            ......
          }
增加方法:
       //add  20151208 for A160 itx auto call recording start
      public void startRecords(String callinfo) {
        log("yh----startRecord:"+callinfo);
        mRecordHandler.sendMessage(mRecordHandler.obtainMessage(REQUEST_START_RECORDING, callinfo));
      }
      //add  20151208 for A160 itx auto call recording end

12.packages\services\Telecomm\src\com\mediatek\telecom\recording\Recorder.java 增加方法:

      //add  start
     public void startRecordings(int outputfileformat, String extension ,String callinfo) throws IOException {
       log("startRecording");
       stop();
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH.mm.ss");
       String prefix = dateFormat.format(new Date());
       File sampleDir = new File(StorageManagerEx.getDefaultPath());
       if (!sampleDir.canWrite()) {
           Log.i(TAG, "----- file can't write!! ---");
           // Workaround for broken sdcard support on the device.
           sampleDir = new File("/sdcard/sdcard");
       }
       Log.i(TAG, "yh---- startRecording ---callinfo"+callinfo);
       sampleDir = new File(sampleDir.getAbsolutePath() + "/PhoneRecord/"+callinfo);
       if (!sampleDir.exists()) {
           sampleDir.mkdirs();
       }
       /// For ALPS01000670. @{
       // get the current path where saved recording files.
       mRecordStoragePath = sampleDir.getCanonicalPath();
       /// @}
       try {
           mSampleFile =new File(sampleDir,prefix+extension);
       } catch (Exception e) {
           setError(SDCARD_ACCESS_ERROR);
           Log.i(TAG, "----***------- can't access sdcard !! " + e);
           throw e;
       }
       log("finish creating temp file, start to record");
       mRecorder = new MediaRecorder();
       mRecorder.setOnErrorListener(this);
       mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
       mRecorder.setOutputFormat(outputfileformat);
       /// ALPS01426963 @{
       // change record encoder format for AMR_NB to ACC, so that improve the record quality.
       mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
       mRecorder.setAudioChannels(1);
       mRecorder.setAudioEncodingBitRate(24000);
       mRecorder.setAudioSamplingRate(16000);
       /// @}
       mRecorder.setOutputFile(mSampleFile.getAbsolutePath());
       try {
           mRecorder.prepare();
           mRecorder.start();
           mSampleStart = System.currentTimeMillis();
           setState(RECORDING_STATE);
       } catch (IOException exception) {
           log("startRecording, IOException");
           handleException();
           throw exception;
       }
   }
   //add  end

13.在專案中配置: PRODUCT_PROPERTY_OVERRIDES += ro.wind.itx.call=1