mercoledì 14 settembre 2016

Rendere autoaggiornabile app Android su website

Se si ha un'app sul proprio sito web e si vuole rendere aggiornabile:
1 ) L'app legge la propria versione presente nel file build.gradle
2) In un file .txt si scrive l'ultima versione presente online, che poi viene letto dall'app.

Lo script dentro activity viene realizzato cosi':

// funzione 1
   int v1 = getVersionCode(this);

// funzione 2(3)
   String vers_online=downloadText();

   int v2 = Integer.parseInt(vers_online);

// se la nuova versione è maggiore della vecchia
   if(v2>v1) {
Intent updateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("URL/app.apk"));
startActivity(updateIntent);
}
1) Metodo dentro la classe MyActivity (funzione 1):

public static int getVersionCode(Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        return pi.versionCode;
    } catch (PackageManager.NameNotFoundException ex) {}
    return 0;
}


Che viene chiamata con:

int version = getVersionCode(this);
2) Metodi dentro la classe MyActivity
Importante: per connettersi a internet e scaricare i file bisogna settare i permessi su Manifest.xml con
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Funzione che si connette col file txt.
private InputStream openHttpConnection() throws IOException {
 InputStream in = null;
    int response = -1;
   
    URL url = new URL("http://www.miosito.com/file.txt");
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");

      //  Log.e("Connection", "stop1");    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
         }
    } catch (Exception ex) {
      System.out.println("Eccezione: " + ex.getMessage());
        throw new IOException("Error connecting");
    }
    return in;
}
Funzione che scarica la stringa e la legge.
private String downloadText() {
int BUFFER_SIZE = 2000;
    InputStream in = null;
    try {
        in = openHttpConnection();
    } catch (IOException e1) {
        return "";
    }
    String str = "";
    if (in != null) {
        InputStreamReader isr = new InputStreamReader(in);
        int charRead;
        char[] inputBuffer = new char[BUFFER_SIZE];
        try {
            while ((charRead = isr.read(inputBuffer)) > 0) {
               String readString = String.copyValueOf(inputBuffer, 0, charRead);
               str += readString;
               inputBuffer = new char[BUFFER_SIZE];
            }
            in.close();
        } catch (IOException e) {
            return "";
        }
    }

    return str;
}

venerdì 9 settembre 2016

Login Facebook Php

1) Errore: Cross-site request forgery validation failed. Required param "state" missing.

Nel mio caso deriva dal fatto che il login facebook avvenisse via http://url e non http:/www.url


martedì 30 agosto 2016

Scrivere diretto su Android

import android.widget.TextView;


TextView tv = new TextView(this);
tv.setText(Data);
setContentView(tv);


lunedì 29 agosto 2016

Autorizzare la conversione del database access con workbench

Su Visual Basic scrivere
CurrentProject.Connection.Execute “GRANT SELECT ON MSysRelationships TO Admin”

domenica 28 agosto 2016

Debug su cellulare Android Studio

Usando Samsung GT-I9070 versione Androdi 2.3.6.

1) Aggiunto su build.grade nella app

android {
    buildTypes {
        debug {
            debuggable true
        }
2) Aggiunto su src/main/AndroidManifest.xml android:debuggable="true"

3) Installato ADB driver per USB su Android

4) Autorizzato il debug su Mobile sotto Impostazioni - Applicazioni

martedì 21 giugno 2016

Pubblicare su Kindle in HTML


Programmi utili

Kindlegen Programma che sia avvia da shell, converte da HTML a files MOBI o KF8 (formati kindle).
Kindlepreviewer Programma che visualizza anteprime (e converte anche in formato mobi)

Formati
.mobi formato kindle da kindle store su amazon
.kf8 formato piu' avanzato per i kindle fire

File toc.opf
tag principali
manifest tutti i files che fanno parte della pubblicazione
spine elenco degli id segnalati con manifest
guide non obbligatorio

Suggerimenti HTML

Top/bottom margins should be specified in ems so that spacing between paragraphs is easily distinguishable at any font or device size.




domenica 1 maggio 2016

Laravel: usare artisan e migrate senza shell

Si puo' creare una route ad hoc che poi utilizza \Artisan:call, quindi chiamare la pagina

Route::get('/updateapp', function()
{
    \Artisan::call('migrate');
    echo 'migrate complete';
});

Se si vuole sovrascrivere una migration bisogna cambiare la data del file che crea la tabella su migration. Ad esempio:


2014_10_12_000000_create_users_table.php
2014_10_16_000000_create_users_table.php -> con il comando migrate sovrascrive la precedente.

sabato 2 aprile 2016

Basi laravel

routes
stanno in app/Http/routes.php

definiscono i comportamenti per i parametri passati sulla url

Route::get('/welcome', function () {
    return view('welcome');
});

restituisce la vista welcome

giovedì 24 marzo 2016

wp_nav_menu

1) cambiare link al menu

function change_menu($items){
if(!is_home()){
foreach($items as $item) {
if($item->title=="Progetti" || $item->title=="Projects") {
} else {
$item->url = get_bloginfo("url") . "/" . $item->url;
}
}
}
return $items;
}

add_filter('wp_nav_menu_objects', 'change_menu');

domenica 20 marzo 2016

Views bulk operation in Drupal

1) Cambiare la data di creazione su un node usando il modulo VIEWS BULK OPERATION e l'opzione Execute Arbitray Script PHP

$timestamp = strtotime('22-09-2008');
$new_node = node_load($entity->nid);
$new_node->created= $timestamp;
node_save($new_node);


venerdì 18 marzo 2016

Metabox in base al template

su functions.php


if (is_admin()) {

$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
 
// controlla se è un template specifico

  if ($template_file == 'page-template.php') {


// codice che genera i meta box (in questo caso ho usato le classi wpalchemy)
  $custom_select_mb = new WPAlchemy_MetaBox(array
(
'id' => '_pagina_cat',
'title' => 'Select Inputs',
'template' => get_stylesheet_directory() . '/metaboxes/select-meta.php',
));  
  }
 
  }

giovedì 28 gennaio 2016

Text domain per tema figlio

add_action( 'after_setup_theme', function () {
    load_child_theme_textdomain( 'chosen-child', get_stylesheet_directory() . '/languages' );
} );

su file style.css

Text Domain: chosen-child
Domain Path: /languages