Java tutorial
/* * Copyright (C) 2017-2018 Vignesh R * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.vignesh.conf; import android.Manifest; import android.app.AlertDialog; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.provider.CalendarContract; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity { /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information */ // The indices for the projection array above. private static final int PROJECTION_ID_INDEX = 0; private static final String[] CAL_PROJECTION = new String[] { CalendarContract.Calendars._ID // 0 }; private static final String[] INSTANCE_PROJECTION = new String[] { CalendarContract.Instances._ID, CalendarContract.Instances.TITLE, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END, CalendarContract.Instances.EVENT_LOCATION, CalendarContract.Instances.DESCRIPTION, CalendarContract.Instances.EVENT_ID }; private GoogleApiClient client; private final String CONTACT_NUM = "contact"; private final String CALENDAR_NAME = "calendar"; private final String REGEX = "regex"; private final String PREFIX = "prefix"; private final String SUFFIX = "suffix"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume(); List<String> list = new ArrayList<>(); final ListView listView = (ListView) findViewById(R.id.info); Cursor cursor; long time = new Date().getTime(); ContentResolver contentResolver = getContentResolver(); Uri uri = CalendarContract.Calendars.CONTENT_URI; SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); String cal = sharedPreferences.getString(CALENDAR_NAME, getResources().getString(R.string.no_calendar)); String selection = "((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?))"; int hours_before = Integer.valueOf(sharedPreferences.getString("past_hours", "2")); int hours_future = Integer.valueOf(sharedPreferences.getString("future_hours", "24")); String[] selectionArgs = new String[] { cal }; if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(), "No calendar permission", Toast.LENGTH_LONG).show(); finish(); return; } if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(), "No calling permission", Toast.LENGTH_LONG).show(); finish(); return; } cursor = contentResolver.query(uri, CAL_PROJECTION, selection, selectionArgs, null); assert cursor != null; while (cursor.moveToNext()) { long calID = 0; // Get the field values calID = cursor.getLong(PROJECTION_ID_INDEX); Uri.Builder instanceUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon(); ContentUris.appendId(instanceUriBuilder, time - hours_before * 60 * 60 * 1000); ContentUris.appendId(instanceUriBuilder, time + hours_future * 60 * 60 * 1000); Uri instanceUri = instanceUriBuilder.build(); String instanceSelection = "((" + CalendarContract.Instances.CALENDAR_ID + "= ?))"; String[] instanceSelectionArgs = new String[] { "" + calID }; Cursor instanceCursor = contentResolver.query(instanceUri, INSTANCE_PROJECTION, instanceSelection, instanceSelectionArgs, CalendarContract.Instances.BEGIN + " ASC"); assert instanceCursor != null; while (instanceCursor.moveToNext()) { String title = instanceCursor.getString(1); Date begin = new Date(instanceCursor.getLong(2)); Date end = new Date(instanceCursor.getLong(3)); String loc = instanceCursor.getString(4); String desc = instanceCursor.getString(5); String full = title + "\n" + loc + "\n" + desc; Pattern passcodePattern = Pattern .compile(sharedPreferences.getString(REGEX, "(\\d{8})[\\s\\t\\n#]")); Matcher passcodeMatcher = passcodePattern.matcher(full); String list_item = (title + "\n" + "START: " + begin + "\nEND: " + end + "\n"); if (passcodeMatcher.find()) list.add(list_item + "Conf code: " + passcodeMatcher.group().trim() + "\n"); } instanceCursor.close(); } cursor.close(); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getApplicationContext(), R.layout.list_layout, list); assert listView != null; listView.setAdapter(arrayAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String s = (String) parent.getItemAtPosition(position); SharedPreferences sharedPreferences = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); String tel = sharedPreferences.getString(CONTACT_NUM, getResources().getString(R.string.no_contact)); Pattern pattern = Pattern.compile(sharedPreferences.getString(REGEX, "(\\d{8})[\\s\\t\\n#]")); Matcher matcher = pattern.matcher(s); if (matcher.find()) { if (!tel.equals(getResources().getString(R.string.no_contact))) { String[] contact = tel.split(": "); String suffix = sharedPreferences.getString(SUFFIX, ""); if (suffix != null) try { suffix = URLEncoder.encode(suffix, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String prefix = sharedPreferences.getString(PREFIX, ""); if (prefix != null) try { prefix = URLEncoder.encode(prefix, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + contact[1] + "%3B" + prefix + matcher.group() + suffix)); if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { startActivity(intent); } } else { new AlertDialog.Builder(MainActivity.this).setTitle("No contact Selected!") .setMessage("Please Select Conference Call number via Settings Menu").show(); } } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_pick2) { startActivity(new Intent(this, SettingsActivity.class)); } return super.onOptionsItemSelected(item); } }