Android Open Source - HapiPodcastJ Add Channel Activity






From Project

Back to project page HapiPodcastJ.

License

The source code is released under:

GNU General Public License

If you think the Android project HapiPodcastJ listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package info.xuluan.podcast;
//ww w.java  2 s. c o m
import info.xuluan.podcastj.R;
import info.xuluan.podcast.parser.FeedHandler;
import info.xuluan.podcast.parser.FeedParserListener;
import info.xuluan.podcast.provider.Subscription;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddChannelActivity extends HapiActivity implements PodcastTab, Flingable {

  private EditText mUrlText;
  private ProgressDialog progress = null;
  private FeedHandler feed_handler = null;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_channel_activity);
    
    mUrlText = (EditText) findViewById(R.id.urlText);    
    resetUrlField();
    
    Button addButton = (Button) findViewById(R.id.addButton);
    addButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        checkValid(mUrlText.getText().toString());
      }
    });
    
    Button clearButton = (Button) findViewById(R.id.clearButton);
    clearButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        resetUrlField();
      }
    });
    
    TabsHelper.setChannelTabClickListeners(this, R.id.channel_bar_add_button);
    if (PodcastBaseActivity.ENABLE_FLING_TABS)
          findViewById(R.id.topView).setOnTouchListener((new FlingGestureDetector(this).createOnTouchListener()));  
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.add_channel_activity, menu);
      return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
/*    case R.id.backup_channels:
      startActivity(new Intent(this, BackupChannelsActivity.class));
      return true;
    case R.id.search_channels:
      startActivity(new Intent(this, SearchActivity.class));
      return true;
    case R.id.list_channels:
      startActivity(new Intent(this, ChannelsActivity.class));
      return true;
*/    }
    return super.onOptionsItemSelected(item);
  }

  private void resetUrlField() {
    mUrlText.setText("http://");
  }
  
  void checkValid(String value) {
    String url = null;
    int fail_res = 0;
    try {
      url = formatURL(value);

      if (Subscription.getByUrl(getContentResolver(), url) != null)
        fail_res = R.string.dialog_message_url_exist;
    } catch (MalformedURLException e) {
      fail_res = R.string.dialog_message_malformed_url;
    }

    if (fail_res != 0) {
      new AlertDialog.Builder(this).setTitle(
          getResources().getText(R.string.dialog_title_add_sub))
          .setMessage(getResources().getText(fail_res))
          .setPositiveButton(android.R.string.ok,
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                    int whichButton) {
                }
              }).show();
      return;
    }
    this.progress = ProgressDialog.show(this, getResources().getText(
        R.string.dialog_title_loading), getResources().getText(
        R.string.dialog_message_loading), true);
    AsyncTask<String, ProgressDialog, FeedParserListener> asyncTask 
      = new AsyncTask<String, ProgressDialog, FeedParserListener>() {
      String url;

      @Override
      protected FeedParserListener doInBackground(String... params) {

        url = params[0];
        // log.debug("doInBackground URL ="+url);
        feed_handler = new FeedHandler(getContentResolver(),getPrefMaxSize());
        return feed_handler.fetchFeed(url,-1); //TODO
      }

      @Override
      protected void onPostExecute(FeedParserListener result) {

        if (AddChannelActivity.this.progress != null) {
          AddChannelActivity.this.progress.dismiss();
          AddChannelActivity.this.progress = null;
        }
        if (result != null) {
          if(result.resultCode==0){
            Toast.makeText(AddChannelActivity.this, getResources().getString(R.string.success),
                Toast.LENGTH_SHORT).show();    
            addFeed(url, result);

          }else{
            Toast.makeText(AddChannelActivity.this, getResources().getString(result.resultCode),
                Toast.LENGTH_LONG).show();            
          }

        } else {
          Toast.makeText(AddChannelActivity.this, getResources().getString(R.string.fail),
              Toast.LENGTH_LONG).show();
        }
      }
    };
    asyncTask.execute(url);
  }

  private void addFeed(String url, FeedParserListener feed) {

    Subscription sub = new Subscription(url);
    sub.subscribe(getContentResolver());
    feed_handler.updateFeed(sub, feed);

  }

  private String formatURL(String value) throws MalformedURLException {
    Pattern p = Pattern.compile("^(http|https)://.*",
        Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(value);
    if (m.find()) {
      URL url = new URL(value);
      return url.toString();
    }

    return null;
  }
  
    private int getPrefMaxSize() {
    SharedPreferences pref = getSharedPreferences(
        Pref.HAPI_PREFS_FILE_NAME, Service.MODE_PRIVATE);
    return  Integer.parseInt(pref.getString(
        "pref_max_new_items", "10"));
  }      

  //PodcastTab interface
  public int iconResource() { return R.drawable.channel_add_big_pic; }
  public int tabLabelResource(boolean isLandscape) { return R.string.channel_bar_button_add; }

  //Flingable interface
  public Intent nextIntent() { return HomeActivity.nextIntent(this); }
  public Intent prevIntent() { return HomeActivity.prevIntent(this); }
}




Java Source Code List

info.xuluan.podcast.AddChannelActivity.java
info.xuluan.podcast.BackupChannelsActivity.java
info.xuluan.podcast.ChannelActivity.java
info.xuluan.podcast.ChannelDetailsActivity.java
info.xuluan.podcast.ChannelsActivity.java
info.xuluan.podcast.DownloadActivity.java
info.xuluan.podcast.EpisodeDetailsActivity.java
info.xuluan.podcast.EpisodeIcons.java
info.xuluan.podcast.EpisodesActivity.java
info.xuluan.podcast.FlingGestureDetector.java
info.xuluan.podcast.Flingable.java
info.xuluan.podcast.HapiActivity.java
info.xuluan.podcast.HapiListActivity.java
info.xuluan.podcast.HapiPreferenceActivity.java
info.xuluan.podcast.HomeActivity.java
info.xuluan.podcast.MainActivity.java
info.xuluan.podcast.PlayerActivity.java
info.xuluan.podcast.PodcastBaseActivity.java
info.xuluan.podcast.PodcastTab.java
info.xuluan.podcast.Pref.java
info.xuluan.podcast.SearchActivity.java
info.xuluan.podcast.StartupActivity.java
info.xuluan.podcast.TabsHelper.java
info.xuluan.podcast.actionbar.ActionBarHelperBase.java
info.xuluan.podcast.actionbar.ActionBarHelperHoneycomb.java
info.xuluan.podcast.actionbar.ActionBarHelperICS.java
info.xuluan.podcast.actionbar.ActionBarHelper.java
info.xuluan.podcast.actionbar.SimpleMenuItem.java
info.xuluan.podcast.actionbar.SimpleMenu.java
info.xuluan.podcast.fetcher.FeedFetcher.java
info.xuluan.podcast.fetcher.Response.java
info.xuluan.podcast.parser.FeedHandler.java
info.xuluan.podcast.parser.FeedParserHandler.java
info.xuluan.podcast.parser.FeedParserListenerInterface.java
info.xuluan.podcast.parser.FeedParserListener.java
info.xuluan.podcast.parser.FeedParser.java
info.xuluan.podcast.parser.OPMLParserHandler.java
info.xuluan.podcast.parser.SearchItem.java
info.xuluan.podcast.provider.FeedItem.java
info.xuluan.podcast.provider.ItemColumns.java
info.xuluan.podcast.provider.PodcastOpenHelper.java
info.xuluan.podcast.provider.PodcastProvider.java
info.xuluan.podcast.provider.SubscriptionColumns.java
info.xuluan.podcast.provider.Subscription.java
info.xuluan.podcast.service.PlayerService.java
info.xuluan.podcast.service.PodcastService.java
info.xuluan.podcast.utils.DialogMenu.java
info.xuluan.podcast.utils.FileUtils.java
info.xuluan.podcast.utils.IconCursorAdapter.java
info.xuluan.podcast.utils.LabeledFrame.java
info.xuluan.podcast.utils.LockHandler.java
info.xuluan.podcast.utils.Log.java
info.xuluan.podcast.utils.SDCardMgr.java
info.xuluan.podcast.utils.StrUtils.java
info.xuluan.podcast.utils.ZipExporter.java
info.xuluan.podcast.utils.ZipImporter.java