AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.o.callhistoryreader">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/call_logs"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="20dp"
android:background="#8D8D96"
android:gravity="center"
android:textColor="#fff"
android:textSize="15sp"
android:text="Click here to Send "
android:scrollbars="vertical"/>
</LinearLayout>
MainActivity.java
package com.o.callhistoryreader;
import android.Manifest;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.CallLog;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import static android.provider.CalendarContract.CalendarCache.URI;
public class MainActivity extends Activity {
private static final String TAG = "CallLog";
private static final int URL_LOADER = 1;
private TextView callLogsTextView;
private static final String FILE_NAME = "call_logs.txt";
StringBuffer sb;
File file, sdCard, directory;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Log.d(TAG, "onCreate()");
setContentView(R.layout.activity_main);
// initialize(); callLogsTextView = findViewById(R.id.call_logs);
callLogsTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCallDetails();
}
});
//First checking if the app is already having the permission if (requestAllPermission()) {
//If permission is already having then showing the toast Toast.makeText(MainActivity.this, "You already have the permission", Toast.LENGTH_LONG).show();
}
else //If the app has not the permission then asking for the permission requestAllPermission();
}
public static boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
public boolean requestAllPermission(){
boolean permission_granted=false;
// The request code used in ActivityCompat.requestPermissions() // and returned in the Activity's onRequestPermissionsResult() int PERMISSION_ALL = 1;
String[] PERMISSIONS = {
android.Manifest.permission.READ_CONTACTS,
android.Manifest.permission.WRITE_CONTACTS,
android.Manifest.permission.READ_CALL_LOG,
android.Manifest.permission.WRITE_CALL_LOG,
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
};
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
permission_granted=true;
}
return permission_granted;
}
private void getCallDetails() {
sb = new StringBuffer();
Cursor managedCursor =getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Details :");
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number); // mobile number String callType = managedCursor.getString(type); // call type String callDate = managedCursor.getString(date); // call date Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
// textString=textString+"\nPhone Number:--- " + phNumber + " \nCall Type:--- " + dir + " \nCall Date:--- " + callDayTime + " \nCall duration in sec :--- " + callDuration+"\n";
}
managedCursor.close();
// callLogsTextView.setText(sb);
save(sb.toString());
Toast.makeText(getApplicationContext(),"/CallHistoryReader/"+FILE_NAME + " saved",Toast.LENGTH_LONG).show();
sendEmail();
//sendThroughWhatsapp(); Log.e("Agil value --- ", sb.toString());
}
public void save(String textString) {
try {
sdCard = Environment.getExternalStorageDirectory();
directory = new File (sdCard.getAbsolutePath() +
"/CallHistoryReader");
if (!directory.exists())
{
directory.mkdirs();
}
file = new File(directory, FILE_NAME);
if (!file.exists())
{
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(textString);
bw.close();
} catch (FileNotFoundException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
}
public void sendEmail(){
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); //set your subject shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); //set your message
File imageFileToShare = new File(file.getPath());
Uri uri = Uri.fromFile(imageFileToShare);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share File"));
}
}
Comments
Post a Comment