com.meycup.ducksound.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.meycup.ducksound.MainActivity.java

Source

/*
 * Copyright (C) 2016 Meycup
 *
 * 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.meycup.ducksound;

import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    final static public String CLIENT_ID = "YOUR CLIENT ID HERE";

    Toolbar play_bar;
    ListView music_list;
    TextView notfound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play_bar = (Toolbar) findViewById(R.id.player_bar);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            play_bar.setElevation(4);
        }

        music_list = (ListView) findViewById(R.id.music_list);
        notfound = (TextView) findViewById(R.id.notfound);

        BackgroundSearch search = new BackgroundSearch(this, music_list, play_bar, notfound);
        search.execute("");
    }

    public static void about(Context context) {
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle("Sobre o Ducksound");
        alert.setMessage("Ducksound  um buscador de msicas grtis, criado por um geek que ama "
                + "msicas e detesta ir em sites no confiveis para baixar."
                + " O Ducksound foi feito para ser smples e fcil de usar, basta "
                + "pesquisar sua msica e dar um click longo nela para baixar."
                + " O cdigo fonte deste aplicativo est no Github e voc pode contribuir "
                + "com o cdigo ou criar sua prpria verso do Ducksound."
                + " \n\nGithub: github.com/williamspacefire/Ducksound-Android");

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        alert.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        SearchView searchView;

        MenuItem menuItem = menu.findItem(R.id.search);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            searchView = (SearchView) menuItem.getActionView();
        } else {
            searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
        }

        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setQueryHint("Pesquise por sua msica");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.about) {

            about(this);
        }

        return true;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        System.exit(0);
    }
}