Advertisement
OoryaK

Call Log / Oorya

May 25th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.09 KB | None | 0 0
  1. //*********************MAIN ACTIVITY
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. import android.widget.Toast;
  8.  
  9. public class MainActivity extends AppCompatActivity
  10. {
  11.  
  12.     EditText owner, lineNumber, lastPay;
  13.     String lastPayStr;
  14.     UtlFile utlFile;
  15.  
  16.  
  17.     @Override
  18.     protected void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.activity_main);
  21.  
  22.         owner=(EditText)findViewById(R.id.owner);
  23.         lineNumber=(EditText)findViewById(R.id.lineNumber);
  24.         lastPay=(EditText)findViewById(R.id.lastPay);
  25.         utlFile=new UtlFile(this);
  26.  
  27.     }
  28.  
  29.     public void btnAddData(View view)
  30.     {
  31.         lastPayStr=lastPay.getText().toString();
  32.  
  33.         if(!lineNumber.getText().toString().matches("((0[57]|[//+]?[1-9]{3}[57])([0-9]{8}))|(([0][23489]|[//+]?[1-9]{3}[23489])([0-9]{7}))"))
  34.         {
  35.             Toast.makeText(MainActivity.this, "Please type phone number", Toast.LENGTH_SHORT).show();
  36.             return;
  37.         }
  38.         if(!lastPayStr.matches("[0-9]+"))
  39.         {
  40.             Toast.makeText(MainActivity.this, "Please type numbers", Toast.LENGTH_SHORT).show();
  41.             return;
  42.         }
  43.         if(lineNumber.getText().toString().indexOf("050")>=0)
  44.         {
  45.             int newLastPay=Integer.parseInt(lastPay.getText().toString())*2;
  46.             lastPayStr=newLastPay+"";
  47.         }
  48.         utlFile.writer(owner.getText().toString(),lineNumber.getText().toString(), lastPayStr);
  49.  
  50.         owner.setText("");
  51.         lineNumber.setText("");
  52.         lastPay.setText("");
  53.     }
  54.  
  55.     public void btnDisplay(View view)
  56.     {
  57.         utlFile.reader();
  58.  
  59.         Intent displayIntent = new Intent(this, DisplayActivity.class);
  60.         this.startActivity(displayIntent);
  61.     }
  62.  
  63.     public void btnClear(View view)
  64.     {
  65.         utlFile.delete();
  66.     }
  67. }
  68.  
  69. //***************************DISPLAY ACTIVITY
  70. import android.support.v7.app.AppCompatActivity;
  71. import android.os.Bundle;
  72. import android.widget.ListView;
  73. import android.widget.TextView;
  74.  
  75. public class DisplayActivity extends AppCompatActivity
  76. {
  77.     private TextView txt, sumPay, sumCall;
  78.     private ListView myListV;
  79.     private Adapter adapter;
  80.  
  81.     @Override
  82.     protected void onCreate(Bundle savedInstanceState) {
  83.         super.onCreate(savedInstanceState);
  84.         setContentView(R.layout.activity_display);
  85.         setPointer();
  86.  
  87.         myListV=(ListView)findViewById(R.id.myListV);
  88.  
  89.         adapter=new Adapter(this);
  90.         myListV.setAdapter(adapter);
  91.         sumPay.setText("Sum pay: "+UtlFile.getSumPay()+"");
  92.         sumCall.setText("Most calls: "+UtlFile.getSumCall());
  93.     }
  94.  
  95.     private void setPointer()
  96.     {
  97.         txt=(TextView)findViewById(R.id.text);
  98.         sumPay=(TextView)findViewById(R.id.sumPay);
  99.         sumCall=(TextView)findViewById(R.id.sumCall);
  100.  
  101.         txt.setText("Call Logs");
  102.     }
  103. }
  104.  
  105. //****************************UTL FILE
  106. import android.content.Context;
  107. import java.io.FileInputStream;
  108. import java.io.FileNotFoundException;
  109. import java.io.FileOutputStream;
  110. import java.io.IOException;
  111. import java.io.InputStreamReader;
  112. import java.io.OutputStreamWriter;
  113. import java.util.ArrayList;
  114. import java.util.Collections;
  115. import java.util.HashMap;
  116. import java.util.Map;
  117.  
  118. public class UtlFile
  119. {
  120.     private static final String  MY_FILE ="call_list.txt";
  121.     private static final int READ_BLOCK_SIZE = 100;
  122.     public  Context context;
  123.     private FileOutputStream fileos;
  124.     private OutputStreamWriter oswFile;
  125.     private FileInputStream fileIS;
  126.     private InputStreamReader isrFile;
  127.  
  128.     public static String s="";
  129.     private static String tempLog;
  130.  
  131.     public String owner;
  132.     public String linenumber;
  133.     public int lastPay;
  134.  
  135.     public static ArrayList<UtlFile> myLogList =  new ArrayList<UtlFile>();
  136.  
  137.  
  138.  
  139.     public UtlFile(String owner, String linenumber, int lastPay)
  140.     {
  141.         this.owner=owner;
  142.         this.linenumber=linenumber;
  143.         this.lastPay=lastPay;
  144.     }
  145.  
  146.     public UtlFile(Context context)
  147.     {
  148.         this.context=context;
  149.     }
  150.  
  151.  
  152.     public void writer(String owner, String linenumber, String lastPay)
  153.     {
  154.         try
  155.         {
  156.             fileos=context.openFileOutput(MY_FILE, context.MODE_PRIVATE);
  157.             oswFile=new OutputStreamWriter(fileos);
  158.             tempLog+=owner+","+linenumber+","+lastPay+",";
  159.             oswFile.write(tempLog);
  160.             oswFile.close();
  161.  
  162.         }catch (FileNotFoundException e) {
  163.             e.printStackTrace();
  164.         }catch (IOException e) {
  165.             e.printStackTrace();
  166.         }
  167.     }
  168.  
  169.     public void delete()
  170.     {
  171.         try {
  172.             tempLog="";
  173.             fileos=context.openFileOutput(MY_FILE, context.MODE_PRIVATE);
  174.             oswFile=new OutputStreamWriter(fileos);
  175.             oswFile.write("");
  176.  
  177.             oswFile.close();
  178.  
  179.             myLogList.clear();
  180.  
  181.         } catch (IOException e) {
  182.             e.printStackTrace();
  183.         }
  184.     }
  185.  
  186.  
  187.     public void reader()
  188.     {
  189.         try {
  190.             myLogList.clear();
  191.             fileIS= context.openFileInput(MY_FILE);  //pointer for reading the file
  192.             isrFile = new InputStreamReader(fileIS); //create a reader (inputStreamReader)
  193.  
  194.             char[] inputBuffer = new char[READ_BLOCK_SIZE]; //size of buffer to read
  195.             int charRead; // indicate if we have still data to read.....
  196.             while ((charRead=isrFile.read(inputBuffer))>0)  //if we have still data to read
  197.             {
  198.                 String readString=String.copyValueOf(inputBuffer,0,charRead); //we put into string variable the entire char read array
  199.                 s+=readString; //add read string to our string
  200.  
  201.                 inputBuffer = new char[READ_BLOCK_SIZE]; //allocate new buffer to read
  202.             }
  203.             isrFile.close(); //close the connection to the file
  204.  
  205.         }catch (FileNotFoundException e) {
  206.             e.printStackTrace();
  207.         }catch (IOException e) {
  208.             e.printStackTrace();
  209.         }
  210.     }
  211.  
  212.     public ArrayList<UtlFile> getLog()
  213.     {
  214.         int temp=1,temp1=2;
  215.         myLogList.clear();
  216.         String[] logArr = s.split(",");
  217.         if(!s.equals("")) {
  218.             myLogList.clear();
  219.             for (int counter = 0; counter < logArr.length; counter += 3) {
  220.                 myLogList.add(new UtlFile(logArr[counter], logArr[temp], Integer.parseInt(logArr[temp1])));
  221.                 temp += 3;
  222.                 temp1 += 3;
  223.             }
  224.         }
  225.         s="";
  226.  
  227.         return myLogList;
  228.     }
  229.  
  230.  
  231.     public static int getSumPay()
  232.     {
  233.         int sumPay=0;
  234.  
  235.         for(UtlFile pay:myLogList)
  236.         {
  237.             sumPay+=pay.lastPay;
  238.         }
  239.         return sumPay;
  240.     }
  241.  
  242.     public static String getSumCall()
  243.     {
  244.  
  245.         String sumCall="";
  246.  
  247.         Map<String,Integer> map = new HashMap<String, Integer>();
  248.         for(int i=0;i<myLogList.size();i++){
  249.             Integer count = map.get(myLogList.get(i).linenumber);
  250.             map.put(myLogList.get(i).linenumber, count==null?1:count+1);//auto boxing and count
  251.         }
  252.  
  253.         ArrayList<Integer> myInt =  new ArrayList<>();
  254.  
  255.         for(Map.Entry<String,?> log:map.entrySet())
  256.         {
  257.             myInt.add((Integer)log.getValue());
  258.         }
  259.  
  260.         Collections.sort(myInt);
  261.  
  262.         for(Map.Entry<String,?> log:map.entrySet())
  263.         {
  264.             if(log.getValue()==myInt.get(myInt.size()-1))
  265.             {
  266.                 sumCall+=log.getKey()+", ";
  267.             }
  268.         }
  269.  
  270.         return sumCall;
  271.     }
  272.  
  273. }
  274.  
  275.  
  276. //************************************ADAPTER
  277. import android.content.Context;
  278. import android.graphics.Color;
  279. import android.view.View;
  280. import android.view.ViewGroup;
  281. import android.widget.BaseAdapter;
  282. import android.widget.LinearLayout;
  283. import android.widget.TextView;
  284.  
  285. import java.util.ArrayList;
  286.  
  287.  
  288. public class Adapter extends BaseAdapter {
  289.  
  290.     private Context context;
  291.     private UtlFile utlfile;
  292.     static ArrayList<UtlFile> myLogList;
  293.  
  294.  
  295.     public Adapter(Context context)
  296.     {
  297.         this.context=context;
  298.  
  299.         utlfile = new UtlFile(context);
  300.         myLogList=utlfile.getLog();
  301.  
  302.     }
  303.     @Override
  304.     public int getCount() {
  305.         return myLogList.size();
  306.     }
  307.  
  308.     @Override
  309.     public Object getItem(int i) {
  310.         return null;
  311.     }
  312.  
  313.     @Override
  314.     public long getItemId(int i) {
  315.         return 0;
  316.     }
  317.  
  318.     @Override
  319.     public View getView(int i, View view, ViewGroup viewGroup)
  320.     {
  321.         LinearLayout mylayout=new LinearLayout(context);
  322.         mylayout.setOrientation(LinearLayout.HORIZONTAL);
  323.         mylayout.setBackgroundColor(Color.CYAN);
  324.  
  325.         TextView txt=new TextView(context);
  326.         txt.setTextSize(30);
  327.  
  328.         txt.setText("Owner: "+myLogList.get(i).owner+"\n"
  329.                 +"Line number: "+myLogList.get(i).linenumber+"\n"
  330.                 +"Last pay: "+myLogList.get(i).lastPay);
  331.        
  332.         mylayout.addView(txt);
  333.         return mylayout;
  334.     }
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement