Back to project page Gcal_Importer.
The source code is released under:
Apache License
If you think the Android project Gcal_Importer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package net.crappo.android.androics; // w ww. ja v a 2 s .co m import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Locale; import android.net.Uri; import android.os.Bundle; import android.os.Environment; //import android.provider.CalendarContract; //import android.provider.CalendarContract.Calendars; //import android.database.Cursor; //import android.graphics.Color; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.webkit.MimeTypeMap; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import android.accounts.Account; import android.accounts.AccountManager; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.res.Resources; public class TopActivity extends Activity { private static final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private static final int RL_PADDING_TOP = 5; private static final int RL_PADDING_BOTTOM = 10; private static final int LV_PADDING_LEFT = 20; private static final int LV_PADDING_BOTTOM = 3; private static final int BTN_LOAD = 1; // ButtonClickListener???switch? private static final int BTN_NEXT = 9; // ButtonClickListener???switch? private static final int REQ_CODE_BROWSER = 1; // onActivityResult???????????????? private static final int REQ_CODE_FILE_PICKER = 2; // onActivityResult???????????????? private static final String ICS_MIME_TYPE = "text/calendar"; // ICS??????MIME??? private String[] pathArray; // ???????????????????ICS??????path??????????? private ViewHolderAdapter adapter; // ListView???adapter private ListView lv; private ZipInOutMethods zipTool; // icalzip?????????????? private Button btLoad; private Button btNext; private String selectedFilePath = ""; // ListView????Tap?????????????????????path????????? static final Locale locale = new Locale("ja", "JP"); static String pathOfExternalDir = Environment.getExternalStorageDirectory().getPath(); static String pathOfDataDir; // data/Android/packageName/file static String pathOfExtractedDir; // pathOfDataDir + "/" + (???????DirectoryName) static String appName; // ??????????????Android???????????????????? static String packageName; // ??????????????Android???????????????????? Resources resources; Model4Top model; // Calendar???????????????????????????????AsyncTask??????????? ProgressDialog progressBar; TopActivity activityObj = this; boolean loadCompleted = false; // ICS?????ListView???????????????????????????????? @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_top); // ???????ICS??????????????????????????????????? if ( pathOfExternalDir != null ) { // SDcard?????? pathOfDataDir = getExternalFilesDir(null).toString(); } else { // ??????? ApplicationInfo appinfo = getApplicationInfo(); pathOfDataDir = appinfo.dataDir; } zipTool = new ZipInOutMethods(pathOfDataDir); pathOfExtractedDir = zipTool.pathToExtractDir; // ?????????????????????? resources = getResources(); packageName = resources.getResourcePackageName(R.layout.activity_top); appName = resources.getString(R.string.app_name); checkAccount(); // ????????????????????????(??????????) // ICS?????ListView????????????????(onCreate???????????dummy????????????????) pathArray = new String[]{""}; adapter = new ViewHolderAdapter(this, 0, pathArray); lv = (ListView)findViewById(R.id.top_listview); lv.setAdapter(adapter); // [????????]???????????? btLoad = (Button)findViewById(R.id.top_button_load); btLoad.setTag(BTN_LOAD); btLoad.setOnClickListener(new ButtonClickListener()); progressBar = new ProgressDialog(this); // [????]???????????? btNext = (Button)findViewById(R.id.top_button_next); btNext.setEnabled(false); btNext.setTag(BTN_NEXT); btNext.setOnClickListener(new ButtonClickListener()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.top, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.top_menu_get_from_google: // Google Calendar????ICS????(icalzip)????????????????????? Intent intent = new Intent(this, BrowserForDownLoadActivity.class); intent.putExtra("debug", "from TopActivity"); startActivityForResult(intent, REQ_CODE_BROWSER); break; case R.id.top_menu_filepicker: // ??????????????????????????????????????ICS????????????????????? pickerForIcsFile(); break; } return super.onOptionsItemSelected(item); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == REQ_CODE_BROWSER){ if(resultCode == RESULT_OK) { // ????????????????Google Calendar????ICS???????????????????????? Toast.makeText(this, R.string.top_toast_download_comp, Toast.LENGTH_LONG).show(); if (zipTool.extractDownloadedZip(data.getExtras().getString("fileName"))) { // zip???????????? Toast.makeText(this, R.string.top_toast_extract_success, Toast.LENGTH_LONG).show(); btNext.setEnabled(false); // ??????????????????ListView??????????????????????????????unable???????? pathArray = getListFilesOfDir(pathOfExtractedDir).toArray(new String[0]); loadCompleted = false; // ICS?????ListView?????????????????false??????????????? new TopIcsListAsync(activityObj).execute(pathArray); // ListView??????????? } else { // zip????????????????????????Toast?? Toast.makeText(this, R.string.top_toast_extract_failuer, Toast.LENGTH_LONG).show(); } } else { // ICS?????????????????????????????????Toast?? Toast.makeText(this, R.string.top_toast_download_failuer, Toast.LENGTH_LONG).show(); } } else if(requestCode == REQ_CODE_FILE_PICKER) { // FilePicker(API 19?????????????)????????????????? if(resultCode == RESULT_OK) { Uri targetFileUri = data.getData(); // FilePicker???????Intent?????????????Uri??????????? String toPath = pathOfExtractedDir + new File(targetFileUri.getPath()).getName(); // ?????????????? if(toPath.equals(targetFileUri.getPath())) { // ????????????????????????????????????????????????????? Toast.makeText(this, R.string.top_toast_under_management, Toast.LENGTH_LONG).show(); } else { // ???????????????????????? copyToExtractedDir(targetFileUri, toPath); } } } } @Override protected void onResume() { super.onResume(); if(loadCompleted) { pathArray = getListFilesOfDir(pathOfExtractedDir).toArray(new String[0]); loadCompleted = false; new TopIcsListAsync(activityObj).execute(pathArray); } } /* ???????????????????????????????????????????? */ private boolean checkAccount() { boolean ret = false; AccountManager acntMgr = AccountManager.get(this); Account[] accounts = acntMgr.getAccountsByType(packageName); // ??????????????????? if (accounts == null || accounts.length == 0) { Account account = new Account(appName, packageName); acntMgr.addAccountExplicitly(account, "DummyPasswd", null); ret = true; } else { ret = true; } return ret; } /* ListView???????????ICS??????????????????????????????????? */ private ArrayList<String> getListFilesOfDir(String targetDir) { File dirObj = new File(targetDir); ArrayList<String> al = new ArrayList<String>(); if(dirObj.isDirectory()){ File[] files = dirObj.listFiles(); if ( files != null ) { for (File fobj : files) { al.add(fobj.getPath() ); } } else { Log.d("getListFilesOfDir", "files == null"); } } else { Log.d("getListFilesOfDir", "isDirectory() == false : " + targetDir); } return al; } /* ????????????????????????????ICS????????????????????????????????????????????????????????? */ private void pickerForIcsFile() { Intent intent = null; if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) { // API 19???????????????????SimpleFileDialog?????????????????????? SimpleFileDialog FileOpenDialog = new SimpleFileDialog(this, "FileOpen",new SimpleFileDialog.SimpleFileDialogListener() { String m_chosen; @Override public void onChosenDir(String chosenFile) { // The code in this function will be executed when the dialog OK button is pushed m_chosen = chosenFile; // ?????????????????????????iCalendar?MIME???"text/calendar"???????????????????????????? String fileName = new File(m_chosen).getName(); int dotIndex = fileName.lastIndexOf('.'); String ext = (dotIndex>=0) ? fileName.substring(dotIndex + 1) : null; String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.toLowerCase()); if(mime != null && mime.equals(ICS_MIME_TYPE)) { Uri uri = new Uri.Builder().path(m_chosen).build(); String toPath = pathOfExtractedDir + fileName; // Toast.makeText(TopActivity.this, "Chosen File: " + m_chosen, Toast.LENGTH_LONG).show(); copyToExtractedDir(uri, toPath); // ????????????????Uri??????????????????path?String??????? } else { Toast.makeText(TopActivity.this, R.string.top_toast_picker_badchoice + m_chosen, Toast.LENGTH_LONG).show(); } } }); FileOpenDialog.Default_File_Name = "default.ics"; FileOpenDialog.chooseFile_or_Dir(); } else { // API 19???????????????????????????????????????????????????????????? intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType(ICS_MIME_TYPE); startActivityForResult(intent, REQ_CODE_FILE_PICKER); } } /* ???????????????????????????????????? * ?pickerForIcsFile() * ?onActivityResult(int requestCode, int resultCode, Intent data) * ????????????????????ICS????????????????????????????(?????????????????????)? */ private void copyToExtractedDir(Uri fromFUri, String toPath) { // pathOfExtractedDir ?????????????? InputStream in = null; try { if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { in = getContentResolver().openInputStream(fromFUri); } else { in = new FileInputStream(new File(fromFUri.getPath())); } BufferedReader br = new BufferedReader(new InputStreamReader(in)); BufferedWriter bw = new BufferedWriter(new FileWriter(toPath)); String buff = null; while((buff = br.readLine()) != null) bw.write(buff + "\r\n"); br.close(); bw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // ?????????ListView???????????? btNext.setEnabled(false); pathArray = getListFilesOfDir(pathOfExtractedDir).toArray(new String[0]); loadCompleted = false; new TopIcsListAsync(activityObj).execute(pathArray); } /* * ????????ListView?????????????? * ??????????????ICS???????????????????????????????????????????????? * ??????????????????????ListView???adapter???????????????????adapter?????????????????????????????????? */ public void refreshListView(String[] pathArray){ if(pathArray.length == 0) { Toast.makeText(this, R.string.top_toast_load_no_files, Toast.LENGTH_LONG).show(); pathArray = new String[]{""}; } adapter = new ViewHolderAdapter(this, 0, pathArray); lv.setAdapter(adapter); lv.setOnItemClickListener(new ItemOnClick()); } /* [??????]??????[????]?????????????????????????? */ private class ButtonClickListener implements OnClickListener { @Override public void onClick(View v) { Intent intent; switch((Integer)v.getTag()){ case BTN_LOAD: // [??????]?????????????????[????]????????????????????path????ICS??????????? btNext.setEnabled(false); pathArray = getListFilesOfDir(pathOfExtractedDir).toArray(new String[0]); new TopIcsListAsync(activityObj).execute(pathArray); break; case BTN_NEXT: // [????]????????????????????????????????ICS??????????????Activity???????? intent = new Intent(TopActivity.this, ShowEventListActivity.class); intent.putExtra("SELECTED_FILE_PATH", selectedFilePath); startActivity(intent); break; } } } /* ListView????ICS?????????????????? */ private class ItemOnClick implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { selectedFilePath = ((ListView)parent).getAdapter().getItem(position).toString(); clearBackgroundColor(parent); // ListView???????????????????????????? v.setBackgroundColor(0xffbbbbbb); // ?????????????????????????????????????????? if(!selectedFilePath.equals("")) btNext.setEnabled(true); // [????]????????????? } /* ListView???????????????????????????? */ void clearBackgroundColor(AdapterView<?> parent) { if (parent instanceof ViewGroup) { ViewGroup layout = (ViewGroup) parent; for (int i = 0; i < layout.getChildCount(); i++) { layout.getChildAt(i).setBackgroundColor(0xffffffff); } } } } /* ListView???????????????View????????????Holder */ private class ViewHolder { ArrayList<TextView> tvArray = new ArrayList<TextView>(); RelativeLayout rlay; ViewHolder(Context context) { rlay = new RelativeLayout(context); rlay.setPadding(0, RL_PADDING_TOP, 0, RL_PADDING_BOTTOM); rlay.setBackgroundColor(0xffffffff); // ?????(Obtained Date)???????TextView RelativeLayout.LayoutParams lpTrContents = new RelativeLayout.LayoutParams(WC, WC); TextView tv1 = new TextView(context); tv1.setId(R.id.lv_tv1); tvArray.add(tv1); rlay.addView(tv1, lpTrContents); tv1.setPadding(LV_PADDING_LEFT, 0, 0, LV_PADDING_BOTTOM); // ????????(Calendar Name)???????TextView lpTrContents = new RelativeLayout.LayoutParams(WC, WC); TextView tv2 = new TextView(context); tv2.setId(R.id.lv_tv2); tvArray.add(tv2); rlay.addView(tv2, lpTrContents); lpTrContents.addRule(RelativeLayout.BELOW, R.id.lv_tv1); tv2.setPadding(LV_PADDING_LEFT, 0, 0, LV_PADDING_BOTTOM); } } /* ViewHolder?ListView???pathArray?????????????????????????????adapter */ private class ViewHolderAdapter extends ArrayAdapter<String> { ViewHolder holder; public ViewHolderAdapter (TopActivity actObj, int resource, String[] pathArray) { super(actObj, resource, pathArray); } @Override public View getView(int position, View convertView, ViewGroup parent) { // ?????if????ArrayAdapter??????????convertView??????????????????????????????????????????? if (convertView == null) { // ??????layout?convertView???set???? holder = new ViewHolder(activityObj); convertView = holder.rlay; convertView.setTag(holder); } else { // ??????????????????????? holder = (ViewHolder) convertView.getTag(); } File fileObj = new File((String)getItem(position)); if(fileObj.getPath() == null || fileObj.getPath().equals("") ) { // ICS?????????path??????????? for (TextView entry : holder.tvArray) { // Dummy????set???? switch(entry.getId()){ case R.id.lv_tv1: entry.setText(resources.getString(R.string.top_listview_title_date) + ": " + resources.getString(R.string.top_listview_value_unknown)); break; case R.id.lv_tv2: entry.setText(resources.getString(R.string.top_listview_title_name) + ": " + resources.getString(R.string.top_listview_value_unknown)); break; } } } else { // pathArray[position]????????????????? for (TextView entry : holder.tvArray) { switch(entry.getId()){ case R.id.lv_tv1: // ICS???????????(???????TimeStamp?format??????????)??? entry.setText(resources.getString(R.string.top_listview_title_date) + ": " + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", locale).format(fileObj.lastModified())); break; case R.id.lv_tv2: // model???????????????????????(position?????????????)????????????? entry.setText(resources.getString(R.string.top_listview_title_name) + ": " + model.calNames.get(position).getValue()); break; } } } return convertView; } } // /* for Debug */ // private void showChild(View v, StringBuilder sbTabs) { // Log.d("showChild", sbTabs + v.getClass().getSimpleName()); // if (v instanceof ViewGroup) { // ViewGroup layout = (ViewGroup) v; // sbTabs = sbTabs.append(" "); // for (int i = 0; i < layout.getChildCount(); i++) { // showChild(layout.getChildAt(i), new StringBuilder(sbTabs)); // } // } // } // // ?????????????????????????????????? // StringBuilder sbTabs; // sbTabs = new StringBuilder(); // showChild(parent, sbTabs); // Log.v("itemOnClick", "showChild(parent, sbTabs): " + sbTabs); // }